-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallest_subarry_than_x.cpp
53 lines (48 loc) · 1.01 KB
/
Smallest_subarry_than_x.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
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
int sb(int arr[], int n, int x)
{
// Your code goes here
int end = 0, start = 0, cur_sum = 0;
int min_l = n + 1;
while (end < n)
{
while (cur_sum <= x && end < n)
{
cur_sum += arr[end];
end++;
}
while (cur_sum > x && start < n)
{
if (end - start < min_l)
min_l = end - start;
cur_sum -= arr[start];
start++;
}
}
return min_l;
}
};
// { Driver Code Starts.
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n >> x;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
Solution obj;
cout << obj.sb(a, n, x) << endl;
}
return 0;
} // } Driver Code Ends