forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumRange.cpp
More file actions
36 lines (32 loc) · 894 Bytes
/
NumRange.cpp
File metadata and controls
36 lines (32 loc) · 894 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
// https://www.interviewbit.com/problems/numrange/
int Solution::numRange(vector<int> &A, int B, int C) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int i = 0;
int j = 0;
int sum = 0;
int count = 0;
while(i < A.size()){
sum = sum + A[j];
if((sum >= B) && (sum <= C)){
count++;
j++;
}
else if(sum < B){
j++;
}
else if(sum > C){
i++;
j = i;
sum = 0;
}
if(j == A.size()){
sum = 0;
i++;
j = i;
}
}
return count;
}