1. Problem Statement

Given an integer x and an integer array arr[], find the length of the smallest contiguous subarray whose sum is strictly greater than x.

If no such subarray exists, return 0.

Example 1:

Input:

Code
x = 51
arr[] = [1, 4, 45, 6, 0, 19]

Output:

Code
3

Explanation:

The subarray [4, 45, 6] has a sum of 55, which is greater than 51. Its length is 3, which is the smallest possible length.

Example 2:

Input:

Code
x = 100
arr[] = [1, 10, 5, 2, 7]

Output:

Code
0

Explanation:

No subarray in the given array has a sum greater than 100. Therefore, the result is 0.

2. Approach – Sliding Window (Two Pointers)

The problem can be solved efficiently using the Sliding Window technique. Instead of generating every possible subarray and calculating its sum separately, a window is maintained using two pointers.

The right pointer expands the window by adding elements, while the left pointer shrinks the window whenever its sum becomes greater than x.

Since all the array elements are non-negative, shrinking the window will never increase its sum. This property helps to find the smallest valid subarray efficiently.

Algorithm

  1. Initialize left = 0, sum = 0, and minLength = int.MaxValue.
  2. Traverse the array using the right pointer.
  3. Add the current element arr[right] to sum.
  4. If sum > x, the current window is a valid subarray.
  5. Update minLength with the minimum length of the current window.
  6. Remove the element at arr[left] from the sum and increment left to shrink the window.
  7. Repeat the shrinking process as long as sum > x.
  8. After traversing the array, return minLength. If no valid subarray is found, return 0.

3. C# Implementation

C#
class Solution {
    public int smallestSubWithSum(int[] arr, int x) {
        int left = 0;
        int sum = 0;
        int minLength = int.MaxValue;

        for (int right = 0; right < arr.Length; right++) {
            sum += arr[right];

            while (sum > x) {
                minLength = Math.Min(minLength, right - left + 1);
                sum -= arr[left];
                left++;
            }
        }

        return minLength == int.MaxValue ? 0 : minLength;
    }
}

4. Code Explanation

Step 1: Initialize variables

C#
int left = 0;
int sum = 0;
int minLength = int.MaxValue;
  • left – represents the starting index of the current window.
  • sum – stores the sum of elements inside the current window.
  • minLength – stores the minimum valid subarray length found so far. It is initialized to int.MaxValue because no valid subarray has been found yet.

Step 2: Expand the sliding window

C#
for (int right = 0; right < arr.Length; right++) {
    sum += arr[right];

The right pointer traverses the array from left to right. Each element is added to the current window sum.

Whenever a new element is added, the window becomes larger and its sum is updated.

Step 3: Check whether the sum is greater than X

C#
while (sum > x) {

Whenever the current window sum exceeds x, a valid subarray is found.

The while loop is used instead of an if statement because the window may be reduced multiple times to find a shorter valid subarray.

Step 4: Update the minimum length

C#
minLength = Math.Min(minLength, right - left + 1);

The current window length is calculated using:

\[ \text{Length} = \text{right} - \text{left} + 1 \]

The Math.Min() method compares the current window length with the previously stored minimum length and keeps the smaller value.

Step 5: Shrink the window

C#
sum -= arr[left];
left++;

The element at the left pointer is removed from the current sum, and the left pointer moves forward.

This reduces the size of the window and helps find a smaller subarray whose sum is still greater than x.

Step 6: Return the result

C#
return minLength == int.MaxValue ? 0 : minLength;

If minLength remains int.MaxValue, no valid subarray was found, so the method returns 0.

Otherwise, the smallest valid subarray length is returned.

5. Dry Run

Input:

Code
x = 51
arr[] = [1, 4, 45, 6, 0, 19]

Initial values:

  • left = 0
  • sum = 0
  • minLength = int.MaxValue
rightElementSumAction
011Sum is not greater than 51
145Sum is not greater than 51
24550Sum is not greater than 51
3656Valid window [1,4,45,6], length = 4
3-55Remove 1, length = 3
3-51Remove 4, sum is no longer greater than 51
4051Sum is not greater than 51
51970Valid window [45,6,0,19], length = 4
5-25Remove 45, window is no longer valid

Final minimum length = 3

The smallest valid subarray is [4, 45, 6], whose sum is 55.

Sliding Window Visualization

Original array

1

4

45

6

0

19

[4, 45, 6] → Sum = 55 → Length = 3

The window shrinks from the left until the sum is no longer greater than 51.

6. Complexity Analysis

  • Time Complexity: O(n) – Both left and right pointers move forward at most n times. Therefore, the array is traversed efficiently without generating all possible subarrays.
  • Auxiliary Space: O(1) – Only a few variables (left, right, sum, and minLength) are used. No additional data structure is required.

7. Key Takeaway

The Sliding Window technique is useful for solving subarray problems when the elements are non-negative and the goal is to find a subarray that satisfies a condition such as a target sum, minimum length, or maximum length.

  • Expand the window using the right pointer.
  • Shrink the window using the left pointer.
  • Update the answer whenever the condition is satisfied.

This approach reduces the time complexity from O(n²) in a brute-force approach to O(n).