-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestSubarrayWithSumK.cpp
More file actions
73 lines (69 loc) · 1.84 KB
/
longestSubarrayWithSumK.cpp
File metadata and controls
73 lines (69 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
// this will be the better approach for the array containing positives, negatives and zeros
// if the array contains only positives then there will be a optimal solution
int longestSubarray(vector<int> &nums, int k)
{
int n = nums.size();
long long sum = 0;
int maxLen = 0;
map<long long, int> mpp;
for (int i = 0; i < n; i++)
{
sum += nums[i];
if (sum == k)
{
maxLen = max(maxLen, i + 1);
}
long long rem = sum - k;
if (mpp.find(rem) != mpp.end())
{
int len = i - mpp[rem];
maxLen = max(maxLen, len);
}
if (mpp.find(sum) == mpp.end())
{
mpp[sum] = i;
}
}
return maxLen;
}
// optimal solution for arrays containing only positives
int longestSubarrayContainingPositivesOnly(vector<int> &nums, int k)
{
int n = nums.size();
long long sum = nums[0];
int maxLen = 0;
int left = 0;
int right = 0;
while (right < n)
{
while (left <= right && sum > k)
{
sum -= nums[left];
left++;
}
if (sum == k)
{
maxLen = max(maxLen, right - left + 1);
}
right++;
if (right < n)
{
sum += nums[right];
}
}
return maxLen;
}
int main()
{
vector<int> arr = {94, -33, -13, 40, -82, 94, -33, -13, 40, -82};
int k = 52;
int maxLen = longestSubarray(arr, k);
cout << "Length of Maxixum Subarray with sum " << k << " is " << maxLen<<'\n';
vector<int> a={1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 6, 6, 4, 7, 9, 5, 4, 3, 2, 8, 1, 6, 5, 7, 8 };
int k1=15;
int len=longestSubarrayContainingPositivesOnly(a, k1);
cout << "Length of Maxixum Subarray with sum " << k1 << " is " << len<<'\n';
return 0;
}