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:
x = 51
arr[] = [1, 4, 45, 6, 0, 19]Output:
3Explanation:
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:
x = 100
arr[] = [1, 10, 5, 2, 7]Output:
0Explanation:
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
- Initialize
left = 0,sum = 0, andminLength = int.MaxValue. - Traverse the array using the
rightpointer. - Add the current element
arr[right]tosum. - If
sum > x, the current window is a valid subarray. - Update
minLengthwith the minimum length of the current window. - Remove the element at
arr[left]from the sum and incrementleftto shrink the window. - Repeat the shrinking process as long as
sum > x. - After traversing the array, return
minLength. If no valid subarray is found, return0.
3. C# Implementation
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
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 toint.MaxValuebecause no valid subarray has been found yet.
Step 2: Expand the sliding window
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
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
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
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
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:
x = 51
arr[] = [1, 4, 45, 6, 0, 19]Initial values:
left = 0sum = 0minLength = int.MaxValue
| right | Element | Sum | Action |
|---|---|---|---|
| 0 | 1 | 1 | Sum is not greater than 51 |
| 1 | 4 | 5 | Sum is not greater than 51 |
| 2 | 45 | 50 | Sum is not greater than 51 |
| 3 | 6 | 56 | Valid window [1,4,45,6], length = 4 |
| 3 | - | 55 | Remove 1, length = 3 |
| 3 | - | 51 | Remove 4, sum is no longer greater than 51 |
| 4 | 0 | 51 | Sum is not greater than 51 |
| 5 | 19 | 70 | Valid window [45,6,0,19], length = 4 |
| 5 | - | 25 | Remove 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
leftandrightpointers move forward at mostntimes. Therefore, the array is traversed efficiently without generating all possible subarrays. - Auxiliary Space: O(1) – Only a few variables (
left,right,sum, andminLength) 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
rightpointer. - Shrink the window using the
leftpointer. - 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).


Join the conversation! Your thoughts help the community grow.