Smallest subarray with sum greater than a given value
Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum strictly greater than x.
Examples:
Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]
Output: 3
Explanation: Minimum length subarray is [4, 45, 6]Input: x = 100, arr[] = [1, 10, 5, 2, 7]
Output: 0
Explanation: No subarray exist
Table of Content
[Naive Approach] Using Two Nested Loops – O(n^2) Time and O(1) Space
The idea is to use two nested loops. The outer loop picks a starting element, the inner loop considers all elements (on right side of current start) as ending element. Whenever sum of elements between current start and end becomes greater than x, update the result if current length is smaller than the smallest length so far.
// C++ program to find smallest
// subarray with sum greater than x
#include <bits/stdc++.h>
using namespace std;
// Returns length of smallest subarray
// with sum greater than x. If no such
// subarray exists, returns 0.
int smallestSubWithSum(int x, vector<int> &arr) {
int n = arr.size();
int res = INT_MAX;
// Pick every element as starting point
for (int i = 0; i < n; i++) {
int curr = 0;
for (int j = i; j < n; j++) {
curr += arr[j];
if (curr > x) {
res = min(res, j - i + 1);
break;
}
}
}
// Return 0 if answer does
// not exists.
if (res == INT_MAX)
return 0;
return res;
}
int main() {
vector<int> arr = {1, 4, 45, 6, 10, 19};
int x = 51;
cout << smallestSubWithSum(x, arr);
return 0;
}
// Java program to find smallest
// subarray with sum greater than x
import java.util.*;
class GfG {
// Returns length of smallest subarray
// with sum greater than x. If no such
// subarray exists, returns 0.
static int smallestSubWithSum(int x, int[] arr) {
int n = arr.length;
int res = Integer.MAX_VALUE;
// Pick every element as starting point
for (int i = 0; i < n; i++) {
int curr = 0;
for (int j = i; j < n; j++) {
curr += arr[j];
if (curr > x) {
res = Math.min(res, j - i + 1);
break;
}
}
}
// Return 0 if answer does
// not exists.
if (res == Integer.MAX_VALUE) return 0;
return res;
}
public static void main(String[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
System.out.println(smallestSubWithSum(x, arr));
}
}
# Python program to find smallest
# subarray with sum greater than x
# Returns length of smallest subarray
# with sum greater than x. If no such
# subarray exists, returns 0.
def smallestSubWithSum(x, arr):
n = len(arr)
res = float('inf')
# Pick every element as starting point
for i in range(n):
curr = 0
for j in range(i, n):
curr += arr[j]
if curr > x:
res = min(res, j - i + 1)
break
# Return 0 if answer does
# not exists.
if res == float('inf'):
return 0
return res
if __name__ == "__main__":
arr = [1, 4, 45, 6, 10, 19]
x = 51
print(smallestSubWithSum(x, arr))
// C# program to find smallest
// subarray with sum greater than x
using System;
class GfG {
// Returns length of smallest subarray
// with sum greater than x. If no such
// subarray exists, returns 0.
static int smallestSubWithSum(int x, int[] arr) {
int n = arr.Length;
int res = int.MaxValue;
// Pick every element as starting point
for (int i = 0; i < n; i++) {
int curr = 0;
for (int j = i; j < n; j++) {
curr += arr[j];
if (curr > x) {
res = Math.Min(res, j - i + 1);
break;
}
}
}
// Return 0 if answer does
// not exists.
if (res == int.MaxValue) return 0;
return res;
}
static void Main(string[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
Console.WriteLine(smallestSubWithSum(x, arr));
}
}
// JavaScript program to find smallest
// subarray with sum greater than x
// Returns length of smallest subarray
// with sum greater than x. If no such
// subarray exists, returns 0.
function smallestSubWithSum(x, arr) {
let n = arr.length;
let res = Infinity;
// Pick every element as starting point
for (let i = 0; i < n; i++) {
let curr = 0;
for (let j = i; j < n; j++) {
curr += arr[j];
if (curr > x) {
res = Math.min(res, j - i + 1);
break;
}
}
}
// Return 0 if answer does
// not exists.
if (res === Infinity) return 0;
return res;
}
//driver code
let arr = [1, 4, 45, 6, 10, 19];
let x = 51;
console.log(smallestSubWithSum(x, arr));
Output
3
[Better Approach] – Prefix Sum and Binary Search – O(n Log n) Time and O(n) Space
The idea is to store the prefix sum in an array and then for every index i, perform binary search in the range [i+1, n] to find the minimum index such that preSum[j] > preSum[i] + x.
Below is the step by step of above approach:
- Compute prefix sum in an array preSum[].
- Iterate through preSum[] and find lower bound for x + preSum[i], here lower bound means index of first value greater than x + preSum[i].
- If the lower bound is found and it’s not equal to x i.e., the subarray sum is greater than the x, calculate the length of current subarray and update result if the current result is a smaller value.
// C++ program to find smallest
// subarray with sum greater than x
#include <bits/stdc++.h>
using namespace std;
// Returns the length of the smallest subarray
// with sum greater than or equal to x
int smallestSubWithSum(int x, vector<int> &arr) {
int n = arr.size();
int res = INT_MAX;
vector<int> preSum(n + 1, 0);
// Compute the prefix sums
for (int i = 1; i <= n; i++)
preSum[i] = preSum[i - 1] + arr[i - 1];
// Iterate through each starting index
for (int i = 1; i <= n; i++) {
// Target sum for current subarray
int toFind = x + preSum[i - 1];
// Find the first prefix sum > target
auto bound = lower_bound(preSum.begin(), preSum.end(), toFind);
if (bound != preSum.end() && *bound != toFind) {
int len = bound - (preSum.begin() + i - 1);
res = min(res, len);
}
}
// If subarray does not exists
if (res == INT_MAX)
return 0;
return res;
}
int main() {
vector<int> arr = {1, 4, 45, 6, 10, 19};
int x = 51;
cout << smallestSubWithSum(x, arr);
return 0;
}
// Java program to find smallest
// subarray with sum greater than or equal to x
import java.util.*;
class GfG {
// Returns the length of the smallest subarray
// with sum greater than or equal to x
static int smallestSubWithSum(int x, int[] arr) {
int n = arr.length;
int res = Integer.MAX_VALUE;
int[] preSum = new int[n + 1];
// Compute the prefix sums
for (int i = 1; i <= n; i++) {
preSum[i] = preSum[i - 1] + arr[i - 1];
}
// Iterate through each starting index
for (int i = 1; i <= n; i++) {
// Target sum for current subarray
int toFind = x + preSum[i - 1] + 1;
// Find the first prefix sum > target
int bound = Arrays.binarySearch(preSum, toFind);
if (bound < 0) {
bound = -(bound + 1);
}
if (bound <= n) {
int len = bound - (i - 1);
res = Math.min(res, len);
}
}
// If subarray does not exists
if (res == Integer.MAX_VALUE) return 0;
return res;
}
public static void main(String[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
System.out.println(smallestSubWithSum(x, arr));
}
}
# Python program to find smallest
# subarray with sum greater than or equal to x
from bisect import bisect_left
# Returns the length of the smallest subarray
# with sum greater than or equal to x
def smallestSubWithSum(x, arr):
n = len(arr)
res = float('inf')
preSum = [0] * (n + 1)
# Compute the prefix sums
for i in range(1, n + 1):
preSum[i] = preSum[i - 1] + arr[i - 1]
# Iterate through each starting index
for i in range(1, n + 1):
# Target sum for current subarray
toFind = x + preSum[i - 1] + 1
# Find the first prefix sum > target
bound = bisect_left(preSum, toFind)
if bound <= n:
len_sub = bound - (i - 1)
res = min(res, len_sub)
# If subarray does not exists
if res == float('inf'):
return 0
return res
if __name__ == "__main__":
arr = [1, 4, 45, 6, 10, 19]
x = 51
print(smallestSubWithSum(x, arr))
// C# program to find smallest
// subarray with sum greater than or equal to x
using System;
class GfG {
// Returns the length of the smallest subarray
// with sum greater than or equal to x
static int smallestSubWithSum(int x, int[] arr) {
int n = arr.Length;
int res = int.MaxValue;
int[] preSum = new int[n + 1];
// Compute the prefix sums
for (int i = 1; i <= n; i++) {
preSum[i] = preSum[i - 1] + arr[i - 1];
}
// Iterate through each starting index
for (int i = 1; i <= n; i++) {
// Target sum for current subarray
int toFind = x + preSum[i - 1] + 1;
// Find the first prefix sum > target
int bound = Array.BinarySearch(preSum, toFind);
if (bound < 0) {
bound = ~bound;
}
if (bound <= n) {
int len = bound - (i - 1);
res = Math.Min(res, len);
}
}
// If subarray does not exists
if (res == int.MaxValue) return 0;
return res;
}
static void Main(string[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
Console.WriteLine(smallestSubWithSum(x, arr));
}
}
// JavaScript program to find smallest
// subarray with sum greater than or equal to x
// Returns the length of the smallest subarray
// with sum greater than or equal to x
function smallestSubWithSum(x, arr) {
let n = arr.length;
let res = Infinity;
let preSum = new Array(n + 1).fill(0);
// Compute the prefix sums
for (let i = 1; i <= n; i++) {
preSum[i] = preSum[i - 1] + arr[i - 1];
}
// Iterate through each starting index
for (let i = 1; i <= n; i++) {
// Target sum for current subarray
let toFind = x + preSum[i - 1] + 1;
// Find the first prefix sum > target
let bound = preSum.findIndex(val => val >= toFind);
if (bound !== -1) {
let len = bound - (i - 1);
res = Math.min(res, len);
}
}
// If subarray does not exists
if (res === Infinity) return 0;
return res;
}
//driver code
let arr = [1, 4, 45, 6, 10, 19];
let x = 51;
console.log(smallestSubWithSum(x, arr));
Output
3
[Expected Approach] – Using Two Pointers – O(n) Time and O(1) Space
The idea is to use two pointer approach to maintain a sliding window, where we keep expanding the window by adding elements until the sum becomes greater than x, then we try to minimize this window by shrinking it from the start while maintaining the sum > x condition. This way, we explore all possible subarrays and keep track of the smallest valid length.
// C++ program to find smallest
// subarray with sum greater than x
#include <bits/stdc++.h>
using namespace std;
// Returns the length of the smallest subarray
// with sum greater than or equal to x
int smallestSubWithSum(int x, vector<int>& arr) {
int i = 0, j = 0;
int sum = 0;
int ans = INT_MAX;
while (j < arr.size()) {
// Expand window until sum > x
// or end of array reached
while (j < arr.size() && sum <= x) {
sum += arr[j++];
}
// If we reached end of array and sum
// still <= x, no valid subarray exists
if (j == arr.size() && sum <= x) break;
// Minimize window from start
// while maintaining sum > x
while (i < j && sum - arr[i] > x) {
sum -= arr[i++];
}
ans = min(ans, j-i);
// Remove current start
// element and shift window
sum -= arr[i];
i++;
}
// Return 0 if no valid subarray
// found, else return min length
if (ans == INT_MAX) return 0;
return ans;
}
int main() {
vector<int> arr = {1, 4, 45, 6, 10, 19};
int x = 51;
cout<<smallestSubWithSum(x, arr);
return 0;
}
// Java program to find smallest
// subarray with sum greater than x
import java.util.*;
class GfG {
// Returns the length of the smallest subarray
// with sum greater than or equal to x
static int smallestSubWithSum(int x, int[] arr) {
int i = 0, j = 0;
int sum = 0;
int ans = Integer.MAX_VALUE;
while (j < arr.length) {
// Expand window until sum > x
// or end of array reached
while (j < arr.length && sum <= x) {
sum += arr[j++];
}
// If we reached end of array and sum
// still <= x, no valid subarray exists
if (j == arr.length && sum <= x) break;
// Minimize window from start
// while maintaining sum > x
while (i < j && sum - arr[i] > x) {
sum -= arr[i++];
}
ans = Math.min(ans, j - i);
// Remove current start
// element and shift window
sum -= arr[i];
i++;
}
// Return 0 if no valid subarray
// found, else return min length
if (ans == Integer.MAX_VALUE) return 0;
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
System.out.println(smallestSubWithSum(x, arr));
}
}
# Python program to find smallest
# subarray with sum greater than x
# Returns the length of the smallest subarray
# with sum greater than or equal to x
def smallestSubWithSum(x, arr):
i, j = 0, 0
sum = 0
ans = float('inf')
while j < len(arr):
# Expand window until sum > x
# or end of array reached
while j < len(arr) and sum <= x:
sum += arr[j]
j += 1
# If we reached end of array and sum
# still <= x, no valid subarray exists
if j == len(arr) and sum <= x:
break
# Minimize window from start
# while maintaining sum > x
while i < j and sum - arr[i] > x:
sum -= arr[i]
i += 1
ans = min(ans, j - i)
# Remove current start
# element and shift window
sum -= arr[i]
i += 1
# Return 0 if no valid subarray
# found, else return min length
if ans == float('inf'):
return 0
return ans
if __name__ == "__main__":
arr = [1, 4, 45, 6, 10, 19]
x = 51
print(smallestSubWithSum(x, arr))
// C# program to find smallest
// subarray with sum greater than x
using System;
class GfG {
// Returns the length of the smallest subarray
// with sum greater than or equal to x
static int smallestSubWithSum(int x, int[] arr) {
int i = 0, j = 0;
int sum = 0;
int ans = int.MaxValue;
while (j < arr.Length) {
// Expand window until sum > x
// or end of array reached
while (j < arr.Length && sum <= x) {
sum += arr[j++];
}
// If we reached end of array and sum
// still <= x, no valid subarray exists
if (j == arr.Length && sum <= x) break;
// Minimize window from start
// while maintaining sum > x
while (i < j && sum - arr[i] > x) {
sum -= arr[i++];
}
ans = Math.Min(ans, j - i);
// Remove current start
// element and shift window
sum -= arr[i];
i++;
}
// Return 0 if no valid subarray
// found, else return min length
if (ans == int.MaxValue) return 0;
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 4, 45, 6, 10, 19};
int x = 51;
Console.WriteLine(smallestSubWithSum(x, arr));
}
}
// JavaScript program to find smallest
// subarray with sum greater than x
// Returns the length of the smallest subarray
// with sum greater than or equal to x
function smallestSubWithSum(x, arr) {
let i = 0, j = 0;
let sum = 0;
let ans = Infinity;
while (j < arr.length) {
// Expand window until sum > x
// or end of array reached
while (j < arr.length && sum <= x) {
sum += arr[j++];
}
// If we reached end of array and sum
// still <= x, no valid subarray exists
if (j === arr.length && sum <= x) break;
// Minimize window from start
// while maintaining sum > x
while (i < j && sum - arr[i] > x) {
sum -= arr[i++];
}
ans = Math.min(ans, j - i);
// Remove current start
// element and shift window
sum -= arr[i];
i++;
}
// Return 0 if no valid subarray
// found, else return min length
if (ans === Infinity) return 0;
return ans;
}
//driver code
let arr = [1, 4, 45, 6, 10, 19];
let x = 51;
console.log(smallestSubWithSum(x, arr));
Output
3
Why is the time complexity O(n)? If you take a closer look, you can notice that every item goes inside the window at most once and goes out of the window at most once. Adding and removing an item takes O(1) time. So we overall do at-most 2n work. Hence the time complexity is O(n).
How to handle negative numbers? The above solution may not work if input array contains negative numbers. For example arr[] = {- 8, 1, 4, 2, -6}. To handle negative numbers, add a condition to ignore subarrays with negative sums. We can use the solution discussed in Find subarray with given sum with negatives allowed in constant space.