-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsubarray_with_given_sum.cpp
33 lines (29 loc) · 1.05 KB
/
subarray_with_given_sum.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
// problem link: https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1/?category[]=sliding-window#
// window sliding technique
vector<int> subarraySum(int arr[], int n, int s)
{
// using window sliding technique
long long int sum=arr[0],start;
vector<int> ans;
start=0;
for(int i=1; i<=n; i++)
{
while(sum > s && start < i-1)
{
sum-=arr[start]; // if sum is greater than the required sum than we decrement the starting elements
start++;
}
if(sum == s) // if sum = required sum than returning the function back
{
ans.push_back(start+1);
ans.push_back(i);
return ans;
}
if(i < n) // if iterator has not reached to last element that expanding the window by adding new elements
{
sum+=arr[i];
}
}
ans.push_back(-1);
return ans;
}