def subarr_min_len(arr, x):
n = len(arr)
i, j = 0, 0
sum = 0
min_len = float('inf')
while j < n:
# move right pointer to increase the sum,
# when the sum is greater than x, the elements
# after current j are ignored.
while j < n and sum <= x:
sum += arr[j]
j = j + 1
# when we reach the end of the array,
# and the sum is smaller than x,
# the rest of cases are ignored.
if j == n and sum <= x:
break
# move left pointer to decrease the sum,
# when the sum is smaller than x, the element
# before current i are ignored.
while i < j and sum - arr[i] > x:
sum -= arr[i]
i = i + 1
min_len = min(min_len, j - i)
# move left pointer
sum -= arr[i]
i += 1
if min_len == float('inf'):
return 0
return min_len
# Driver code
arr = [1, 4, 45, 6, 10, 19]
x = 51
print(subarr_min_len(arr, x))