Skip to content

Commit 83c101a

Browse files
committed
🚀 28-Jun-2020
1 parent c1807a9 commit 83c101a

File tree

9 files changed

+419
-0
lines changed

9 files changed

+419
-0
lines changed

array/generate_all_subarrays.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
A subbarray is a contiguous part of array. An array that is inside another array. For example, consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subbarays are (1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4) and (1,2,3,4). In general, for an array/string of size n, there are n*(n+1)/2 non-empty subarrays/subsrings.
3+
*/
4+
5+
6+
7+
8+
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
void allSubArray(int a[], int n)
14+
{
15+
for (int i=0; i <n; i++){
16+
for (int j=i; j<n; j++){
17+
for (int k=i; k<=j; k++)
18+
cout << a[k] << " ";
19+
cout << endl;
20+
}
21+
}
22+
}
23+
24+
int main()
25+
{ int n=4;
26+
int a[] = {1, 2, 3, 4};
27+
allSubArray(a, n);
28+
return 0;
29+
}

array/largest_subarray_with_sum_0.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
Given an array having both positive and negative integers. The task is to compute the length of the largest subarray with sum 0.
4+
5+
Input:
6+
The first line of input contains an element T denoting the number of test cases. Then T test cases follow. Each test case consists of 2 lines. The first line of each test case contains a number denoting the size of the array A. Then in the next line are space-separated values of the array A.
7+
8+
Output:
9+
For each test case, the output will be the length of the largest subarray which has sum 0.
10+
11+
User Task:
12+
Since this is a functional problem you don't have to worry about input, you just have to complete the function maxLen() which takes two arguments an array A and n, where n is the size of the array A and returns the length of the largest subarray with 0 sum.
13+
14+
Expected Time Complexity: O(N*Log(N)).
15+
Expected Auxiliary Space: O(N).
16+
17+
Constraints:
18+
1 <= T <= 100
19+
1 <= N <= 104
20+
-1000 <= A[i] <= 1000, for each valid i
21+
22+
Example:
23+
Input
24+
1
25+
8
26+
15 -2 2 -8 1 7 10 23
27+
28+
Output
29+
5
30+
31+
Explanation
32+
Testcase 1: In the above test case the largest subarray with sum 0 will be -2 2 -8 1 7.
33+
*/
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
#include <bits/stdc++.h>
46+
using namespace std;
47+
48+
int maxLen(int A[], int n);
49+
50+
int main()
51+
{
52+
int T;
53+
cin >> T;
54+
while (T--)
55+
{
56+
int N;
57+
cin >> N;
58+
int A[N];
59+
for (int i = 0; i < N; i++)
60+
cin >> A[i];
61+
cout << maxLen(A, N) << endl;
62+
}
63+
}
64+
65+
66+
67+
68+
int maxLen(int a[], int n)
69+
{
70+
int maxLen=0;
71+
unordered_map<int, int> m;
72+
int sum=0;
73+
for(int i=0;i<n;i++){
74+
sum+=a[i];
75+
if(sum==0) maxLen=max(maxLen, i+1);
76+
if(m.find(sum)!=m.end()) maxLen=max(maxLen, i-m[sum]);
77+
else m.insert({sum, i});
78+
}
79+
return maxLen;
80+
}

array/three_sum_problem.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Given an array A[] of N numbers and another number x, determine whether or not there exist three elements in A[] whose sum is exactly x.
3+
4+
Input:
5+
First line of input contains number of testcases T. For each testcase, first line of input contains n and x. Next line contains array elements.
6+
7+
Output:
8+
Print 1 if there exist three elements in A whose sum is exactly x, else 0.
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ N ≤ 103
13+
1 ≤ A[i] ≤ 105
14+
15+
Example:
16+
Input:
17+
2
18+
6 13
19+
1 4 45 6 10 8
20+
5 10
21+
1 2 4 3 6
22+
23+
Output:
24+
1
25+
1
26+
27+
Explanation:
28+
Testcase 1: There is one triplet with sum 13 in the array. Triplet elements are 1, 4, 8, whose sum is 13.
29+
*/
30+
31+
32+
33+
34+
35+
36+
37+
#include<bits/stdc++.h>
38+
using namespace std;
39+
40+
41+
int threeSum(int a[], int n, int x){
42+
sort(a, a+n);
43+
for(int i=0;i<n-2;i++){
44+
int reqSum=x-a[i];
45+
int l=i+1, r=n-1;
46+
while(l<r){
47+
else if(a[l]+a[r]<reqSum) l++;
48+
else r--;
49+
}
50+
}
51+
return 0;
52+
}
53+
54+
int main(){
55+
ios_base::sync_with_stdio(false);
56+
cin.tie(NULL);
57+
cout.tie(NULL);
58+
int t;
59+
cin>>t;
60+
while(t--){
61+
int n, x;
62+
cin>>n>>x;
63+
int a[n];
64+
for(int i=0;i<n;i++) cin>>a[i];
65+
cout<<threeSum(a, n, x)<<endl;
66+
}
67+
68+
return 0;
69+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
2+
3+
Note:
4+
5+
The solution set must not contain duplicate triplets.
6+
7+
Example:
8+
9+
Given array nums = [-1, 0, 1, 2, -1, -4],
10+
11+
A solution set is:
12+
[
13+
[-1, 0, 1],
14+
[-1, -1, 2]
15+
]
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
class Solution {
27+
public:
28+
vector<vector<int>> threeSum(vector<int>& nums) {
29+
vector<vector<int> > res;
30+
int n=nums.size();
31+
if(n<3) return res;
32+
33+
sort(nums.begin(), nums.end());
34+
35+
for(int i=0;i<n-2;i++){
36+
if(i==0 || (i>0 && nums[i]!=nums[i-1])){ // not taking duplicates
37+
int sum=0-nums[i];
38+
int low=i+1, high=n-1;
39+
while(low<high){
40+
if(nums[low]+nums[high]==sum){
41+
res.push_back({nums[i], nums[low], nums[high]});
42+
while(low<high && nums[low]==nums[low+1]) low++;
43+
while(low<high && nums[high]==nums[high-1]) high--;
44+
low++; high--;
45+
} else if(nums[low]+nums[high]>sum) high--;
46+
else low++;
47+
}
48+
49+
}
50+
}
51+
return res;
52+
}
53+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
2+
3+
Note:
4+
5+
The solution set must not contain duplicate quadruplets.
6+
7+
Example:
8+
9+
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
10+
11+
A solution set is:
12+
[
13+
[-1, 0, 0, 1],
14+
[-2, -1, 1, 2],
15+
[-2, 0, 0, 2]
16+
]
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
class Solution {
28+
public:
29+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
30+
vector<vector<int> > res;
31+
int n=nums.size();
32+
if(n<4) return res;
33+
34+
sort(nums.begin(), nums.end());
35+
36+
for(int i=0;i<n-3;i++){
37+
if(i==0 || (i>0 && nums[i]!=nums[i-1])){ // check duplicates
38+
int k1=target-nums[i];
39+
for(int j=i+1;j<n-2;j++){
40+
if(j==i+1 || (j>i+1 && nums[j]!=nums[j-1])){ // check duplicates
41+
int k2=k1-nums[j];
42+
int low=j+1, high=n-1;
43+
while(low<high){
44+
if(nums[low]+nums[high]==k2){
45+
res.push_back({nums[i], nums[j], nums[low], nums[high]});
46+
while(low<high && nums[low]==nums[low+1]) low++;
47+
while(low<high && nums[high]==nums[high-1]) high--;
48+
low++;
49+
high--;
50+
}
51+
else if(nums[low]+nums[high]>k2) high--;
52+
else low++;
53+
}
54+
}
55+
56+
}
57+
}
58+
}
59+
return res;
60+
}
61+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
3+
4+
Example 1:
5+
6+
Input: n = 12
7+
Output: 3
8+
Explanation: 12 = 4 + 4 + 4.
9+
Example 2:
10+
11+
Input: n = 13
12+
Output: 2
13+
Explanation: 13 = 4 + 9.
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
int numSquares(int n) {
24+
if(n<=3) return n;
25+
int dp[n+1];
26+
27+
for(int i=0;i<=n;i++){
28+
dp[i]=i;
29+
for(int j=1;j*j<=i;j++){
30+
int sq=j*j;
31+
dp[i]=min(dp[i], 1+dp[i-sq]);
32+
}
33+
}
34+
return dp[n];
35+
}
36+
};
37+
38+
39+
40+
41+
42+
43+
44+
45+
//Recursion
46+
class Solution {
47+
int solve(int n)
48+
{
49+
if(n<=3)
50+
return n;
51+
52+
int ans=n;
53+
for(int i=1;i*i<=n;++i)
54+
ans = min(ans,1+solve(n-i*i));
55+
56+
return ans;
57+
}
58+
public:
59+
int numSquares(int n) {
60+
int ans = solve(n);
61+
return ans;
62+
}
63+
};

0 commit comments

Comments
 (0)