forked from sauravgpt2/hacktoberfest36_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarray_with_Sum_k.cpp
More file actions
48 lines (45 loc) · 957 Bytes
/
Subarray_with_Sum_k.cpp
File metadata and controls
48 lines (45 loc) · 957 Bytes
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
#include <iostream>
using namespace std;
int main()
{
int n,start=0,end,k,s,d=0;
cin >>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin >> a[i];
}
int curr_sum=0;
for(int end=0;end<=n;)
{
if(end==n)
{
end=n-1;
d++;
}
if(curr_sum < k)
{
curr_sum=curr_sum+a[end];
end++;
}
else if(curr_sum > k)
{
curr_sum=curr_sum-a[start];
start=start+1;
}
else if(curr_sum==k)
{
if(d==0)
{
s=end-1;
}
if(d!=0)
{
s=n-1;
}
end=n+1;
}
}
cout <<start <<" "<<s;
return 0;
}