forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smallest subarray with sum greater than x.cpp
58 lines (48 loc) · 1.2 KB
/
Smallest subarray with sum greater 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
54
55
56
57
58
/*
Smallest subarray with sum greater than x
==========================================
Given an array of integers (A[]) and a number x, find the smallest subarray with sum greater than the given value.
Note: The answer always exists. It is guaranteed that x doesn't exceed the summation of a[i] (from 1 to N).
Example 1:
Input:
A[] = {1, 4, 45, 6, 0, 19}
x = 51
Output: 3
Explanation:
Minimum length subarray is
{4, 45, 6}
Example 2:
Input:
A[] = {1, 10, 5, 2, 7}
x = 9
Output: 1
Explanation:
Minimum length subarray is {10}
Your Task:
You don't need to read input or print anything. Your task is to complete the function sb() which takes the array A[], its size N and an integer X as inputs and returns the required ouput.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N, x ≤ 105
1 ≤ A[] ≤ 104
*/
int sb(int arr[], int n, int x)
{
int curr_sum = 0, min_len = n + 1;
int start = 0, end = 0;
while (end < n)
{
while (curr_sum <= x && end < n)
{
curr_sum += arr[end];
end++;
}
while (curr_sum > x && start < n)
{
min_len = min(min_len, end - start);
curr_sum -= arr[start];
start++;
}
}
return min_len;
}