forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubarray with given sum.cpp
54 lines (49 loc) · 958 Bytes
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
//code
int T, t; //T is the no. of test cases
cin>>T;
long curr_sum, value;
int i, start;
int n, x, f=0; //n is no. of elements in the array
vector<int> N; //N stores the input array
for(t=0; t<T; t++) //loop for test cases
{
cin>>n>>value;
for(i=0; i<n; i++)
{
cin>>x;
N.push_back(x);
}
start = 0;
curr_sum = N[0];
f=0;
for(i=1; i<=n; i++)
{
while(curr_sum > value && start <= i-1)
{
curr_sum -= N[start];
start++;
}
if(curr_sum == value)
{
cout<<start+1<<" "<<i<<endl;
f=1;
break;
}
if(curr_sum < value && i<n)
{
curr_sum += N[i];
}
}
if(f==0)
{
cout<<(-1)<<endl;
}
N.clear();
}
return 0;
}