Smallest Subarray with Sum Greater Than X

Given an array of positive integers `arr` and a number `X`, find the **minimum length** of a contiguous subarray such that the sum of its elements is strictly greater than `X`. If no such subarray exists, return 0.

Input Format

arr = array of positive integers, X = target sum

Output Format

integer (length of smallest subarray with sum > X, or 0 if none)

Constraints

  • 1 <= arr.length <= 10^5; 1 <= arr[i] <= 10^4; 1 <= X <= 10^9

Examples

Example 1:

Input:

arr = [1,4,45,6,10,19]
X = 51

Output:

3

Example 2:

Input:

arr = [1,2,4,1,3]
X = 7

Output:

3
Loading...
Smallest Subarray with Sum Greater Than X