1def smallestSubWithSum(arr, n, x):
2
3 # Initialize current sum and minimum length
4 curr_sum = 0
5 min_len = n + 1
6
7 # Initialize starting and ending indexes
8 start = 0
9 end = 0
10 while (end < n):
11
12 # Keep adding array elements while current
13 # sum is smaller than or equal to x
14 while (curr_sum <= x and end < n):
15 curr_sum += arr[end]
16 end += 1
17
18 # If current sum becomes greater than x.
19 while (curr_sum > x and start < n):
20
21 # Update minimum length if needed
22 if (end - start < min_len):
23 min_len = end - start
24
25 # remove starting elements
26 curr_sum -= arr[start]
27 start += 1
28
29 return min_len