-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumRange.cpp
More file actions
54 lines (42 loc) · 1.3 KB
/
numRange.cpp
File metadata and controls
54 lines (42 loc) · 1.3 KB
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 <iostream>
#include <vector>
#include <queue>
using namespace std;
int 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 mini = min(B,C);
int maxi = max(B,C);
int sum=0;
queue<int> q;
int count = 0;
for (int i=0; i < A.size(); ++i) {
cout<<"--------"<<endl<<A[i]<<endl<<"----------"<<endl;
q.push(A[i]);
if (A[i]>maxi) {
while (!q.empty()){
q.pop();
sum = 0;
}
continue;
}
sum = sum + A[i];
if (sum < mini) continue;
//cout<<sum<<endl;
while (sum > maxi) {
int val = q.front();q.pop();
cout<<val<<" "<<sum<<endl;
sum -= val;
}
cout<<"Sum+"<<sum<<endl;
++count;
}
return count;
}
int main(){
vector<int> v = {80, 97, 78, 45, 23, 38, 38, 93, 83, 16, 91, 69, 18, 82, 60, 50, 61, 70, 15, 6, 52, 90};
cout<<numRange(v,99,269);
}