-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSubarray with given sum.cpp
53 lines (47 loc) · 1.3 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Problem Statement : https://practice.geeksforgeeks.org/problems/subarray-with-given-sum/0/?track=SP-Arrays%20and%20Searching
Asked in : Visa,Google,Facebook,Amazon
Code By:
Abhinav,
CSE, NIT P
(Masters_Abh on codechef, codeforces, hackerrank and Spoj)
*/
#include <bits/stdc++.h>
using namespace std;
#define lint long long int
int main() {
lint t;
cin>>t;
while(t--){
lint n,k;
cin>>n>>k;
lint a[n];
for(lint i=0;i<n;i++){
cin>>a[i];
}
lint left=1,right=1,sum=a[0]; // Maintain left and right index of the range
while(right<=n){
if(sum>k){
sum-=a[left-1]; // If the sum goes larger than expected value, shift a position from left
left++;
}
else if(sum==k){
cout<<left<<" "<<right; // When the expected sum is found
left=-1;
break;
}
else{
right++;
if(right>n){ // Else, continue to shift the right index forward
break;
}
sum+=a[right-1];
}
}
if(left!=-1){ // When no range is found for the expected sum
cout<<-1;
}
cout<<endl;
}
return 0;
}