-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.cpp
54 lines (46 loc) · 1.1 KB
/
day10.cpp
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
#include<bits/stdc++.h>
using namespace std;
// bruete force approach
//int getLongestSubarray(vector<int> &a, long long k) {
// int n = a.size();
//
// int len = 0;
// for (int i = 0; i < n; i++) {
// long long s = 0;
// for (int j = i; j < n; j++) {
// s += a[j];
// if (s == k)
// len = max(len, j - i + 1);
// }
// }
// return len;
//}
//optimal solution
int getLongestSubarray(vector<int> &a, long long k) {
int right = 0;
int left = 0;
long long sum = 0;
int maxLen = 0;
int n = a.size();
while (right < n) {
if (right < n) {
sum += a[right];
}
while (left <= right && sum > k) {
sum -= a[left];
left++;
}
if (sum == k) {
// maxLen = max(maxLen, (right - left + 1)); // for longest array
maxLen++; // for the total number of max array of subarray
}
right++;
}
return maxLen;
}
int main() {
vector<int> arr = {1, 1, 1};
int k = 0;
cout << getLongestSubarray(arr, k);
return 0;
}