Skip to content

Commit abc5cc9

Browse files
solved on 14/9/23
1 parent 0de01be commit abc5cc9

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"sqroot": "python",
4+
"ostream": "cpp",
5+
"iostream": "cpp"
6+
}
7+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int longestSubarrayWithSumK(vector<int> a, long long k)
5+
{
6+
int left = 0, right = 0;
7+
long long sum = a[0];
8+
int maxLen = 0;
9+
int n = a.size();
10+
11+
while (right < n)
12+
{
13+
while (left <= right && sum > k)
14+
{
15+
sum -= a[left];
16+
left++;
17+
}
18+
if (sum == k)
19+
{
20+
maxLen = max(maxLen, right - left + 1);
21+
}
22+
right++;
23+
if (right < n)
24+
sum += a[right];
25+
}
26+
return maxLen;
27+
}
28+
29+
int main()
30+
{
31+
int n;
32+
cout << "Enter the size of the array: ";
33+
cin >> n;
34+
vector<int> arr(n);
35+
cout << "Enter the elements of the array: ";
36+
for (int i = 0; i < n; i++)
37+
cin >> arr[i];
38+
long long k;
39+
cout << "Enter the value of k: ";
40+
cin >> k;
41+
cout << "The length of the longest subarray with sum k is: ";
42+
cout << longestSubarrayWithSumK(arr, k) << endl;
43+
return 0;
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int getLongestSubarray(vector<int> &a, int k)
5+
{
6+
map<int, int> preSumMap;
7+
int sum = 0;
8+
int maxLen = 0;
9+
for (int i = 0; i < a.size(); i++)
10+
{
11+
sum += a[i];
12+
if (sum == k)
13+
{
14+
maxLen = i + 1;
15+
}
16+
int rem = sum - k;
17+
if (preSumMap.find(rem) != preSumMap.end())
18+
{
19+
int len = i - preSumMap[rem];
20+
maxLen = max(len, maxLen);
21+
}
22+
if (preSumMap.find(sum) == preSumMap.end())
23+
{
24+
preSumMap[sum] = i;
25+
}
26+
}
27+
return maxLen;
28+
}
29+
30+
int main()
31+
{
32+
int n;
33+
cout << "Enter the size of the array: ";
34+
cin >> n;
35+
vector<int> arr(n);
36+
cout << "Enter the elements of the array: ";
37+
for (int i = 0; i < n; i++)
38+
cin >> arr[i];
39+
int k;
40+
cout << "Enter the value of k: ";
41+
cin >> k;
42+
cout << "The length of the longest subarray with sum k is: ";
43+
cout << getLongestSubarray(arr, k) << endl;
44+
return 0;
45+
}

CS_28_CountAllSubarraysWithSumK.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int findAllSubarraysWithGivenSum(vector<int> &arr, int k)
5+
{
6+
long long sum = arr[0];
7+
int left = 0, right = 0, count = 0;
8+
int n = arr.size();
9+
10+
while (right < n)
11+
{
12+
while (left <= right && sum > k)
13+
{
14+
sum -= arr[left];
15+
left++;
16+
}
17+
if (sum == k)
18+
count++;
19+
right++;
20+
if (right < n)
21+
sum += arr[right];
22+
}
23+
24+
return count;
25+
}
26+
27+
int main()
28+
{
29+
int n;
30+
cout << "Enter the size of the array: ";
31+
cin >> n;
32+
vector<int> arr(n);
33+
cout << "Enter the elements of the array: ";
34+
for (int i = 0; i < n; i++)
35+
cin >> arr[i];
36+
int k;
37+
cout << "Enter the value of k: ";
38+
cin >> k;
39+
cout << "The number of subarrays with sum k is: ";
40+
cout << findAllSubarraysWithGivenSum(arr, k) << endl;
41+
return 0;
42+
}

0 commit comments

Comments
 (0)