Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum greater than the given value.
Examples:
arr[] = {1, 4, 45, 6, 0, 19}
x = 51
Output: 3
Minimum length subarray is {4, 45, 6}
arr[] = {1, 10, 5, 2, 7}
x = 9
Output: 1
Minimum length subarray is {10}
arr[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250}
x = 280
Output: 4
Minimum length subarray is {100, 1, 0, 200}
arr[] = {1, 2, 4}
x = 8
Output : Not Possible
Whole array sum is smaller than 8.
Naive approach: A simple solution 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 more than the given number, update the result if current length is smaller than the smallest length so far.
Below is the implementation of the above approach:
C++
# include <iostream>
using namespace std;
int smallestSubWithSum( int arr[], int n, int x)
{
int min_len = n + 1;
for ( int start=0; start<n; start++)
{
int curr_sum = arr[start];
if (curr_sum > x) return 1;
for ( int end=start+1; end<n; end++)
{
curr_sum += arr[end];
if (curr_sum > x && (end - start + 1) < min_len)
min_len = (end - start + 1);
}
}
return min_len;
}
int main()
{
int arr1[] = {1, 4, 45, 6, 10, 19};
int x = 51;
int n1 = sizeof (arr1)/ sizeof (arr1[0]);
int res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1+1)? cout << "Not possible\n" :
cout << res1 << endl;
int arr2[] = {1, 10, 5, 2, 7};
int n2 = sizeof (arr2)/ sizeof (arr2[0]);
x = 9;
int res2 = smallestSubWithSum(arr2, n2, x);
(res2 == n2+1)? cout << "Not possible\n" :
cout << res2 << endl;
int arr3[] = {1, 11, 100, 1, 0, 200, 3, 2, 1, 250};
int n3 = sizeof (arr3)/ sizeof (arr3[0]);
x = 280;
int res3 = smallestSubWithSum(arr3, n3, x);
(res3 == n3+1)? cout << "Not possible\n" :
cout << res3 << endl;
return 0;
}
|
Java
import java.io.*;
class SmallestSubArraySum
{
static int smallestSubWithSum( int arr[], int n, int x)
{
int min_len = n + 1 ;
for ( int start = 0 ; start < n; start++)
{
int curr_sum = arr[start];
if (curr_sum > x)
return 1 ;
for ( int end = start + 1 ; end < n; end++)
{
curr_sum += arr[end];
if (curr_sum > x && (end - start + 1 ) < min_len)
min_len = (end - start + 1 );
}
}
return min_len;
}
public static void main(String[] args)
{
int arr1[] = { 1 , 4 , 45 , 6 , 10 , 19 };
int x = 51 ;
int n1 = arr1.length;
int res1 = smallestSubWithSum(arr1, n1, x);
if (res1 == n1+ 1 )
System.out.println( "Not Possible" );
else
System.out.println(res1);
int arr2[] = { 1 , 10 , 5 , 2 , 7 };
int n2 = arr2.length;
x = 9 ;
int res2 = smallestSubWithSum(arr2, n2, x);
if (res2 == n2+ 1 )
System.out.println( "Not Possible" );
else
System.out.println(res2);
int arr3[] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 };
int n3 = arr3.length;
x = 280 ;
int res3 = smallestSubWithSum(arr3, n3, x);
if (res3 == n3+ 1 )
System.out.println( "Not Possible" );
else
System.out.println(res3);
}
}
|
Python3
def smallestSubWithSum(arr, n, x):
min_len = n + 1
for start in range ( 0 ,n):
curr_sum = arr[start]
if (curr_sum > x):
return 1
for end in range (start + 1 ,n):
curr_sum + = arr[end]
if curr_sum > x and (end - start + 1 ) < min_len:
min_len = (end - start + 1 )
return min_len;
arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ]
x = 51
n1 = len (arr1)
res1 = smallestSubWithSum(arr1, n1, x);
if res1 = = n1 + 1 :
print ( "Not possible" )
else :
print (res1)
arr2 = [ 1 , 10 , 5 , 2 , 7 ]
n2 = len (arr2)
x = 9
res2 = smallestSubWithSum(arr2, n2, x);
if res2 = = n2 + 1 :
print ( "Not possible" )
else :
print (res2)
arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ]
n3 = len (arr3)
x = 280
res3 = smallestSubWithSum(arr3, n3, x)
if res3 = = n3 + 1 :
print ( "Not possible" )
else :
print (res3)
|
C#
using System;
class GFG
{
static int smallestSubWithSum( int []arr,
int n, int x)
{
int min_len = n + 1;
for ( int start = 0; start < n; start++)
{
int curr_sum = arr[start];
if (curr_sum > x)
return 1;
for ( int end = start + 1;
end < n; end++)
{
curr_sum += arr[end];
if (curr_sum > x &&
(end - start + 1) < min_len)
min_len = (end - start + 1);
}
}
return min_len;
}
static public void Main ()
{
int []arr1 = {1, 4, 45,
6, 10, 19};
int x = 51;
int n1 = arr1.Length;
int res1 = smallestSubWithSum(arr1,
n1, x);
if (res1 == n1 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res1);
int []arr2 = {1, 10, 5, 2, 7};
int n2 = arr2.Length;
x = 9;
int res2 = smallestSubWithSum(arr2,
n2, x);
if (res2 == n2 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res2);
int []arr3 = {1, 11, 100, 1, 0,
200, 3, 2, 1, 250};
int n3 = arr3.Length;
x = 280;
int res3 = smallestSubWithSum(arr3,
n3, x);
if (res3 == n3 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res3);
}
}
|
Javascript
<script>
function smallestSubWithSum(arr, n, x)
{
let min_len = n + 1;
for (let start=0; start<n; start++)
{
let curr_sum = arr[start];
if (curr_sum > x) return 1;
for (let end=start+1; end<n; end++)
{
curr_sum += arr[end];
if (curr_sum > x && (end - start + 1) < min_len)
min_len = (end - start + 1);
}
}
return min_len;
}
let arr1 = [1, 4, 45, 6, 10, 19];
let x = 51;
let n1 = arr1.length;
let res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1 + 1)? document.write( "Not possible<br>" ) :
document.write(res1 + "<br>" );
let arr2 = [1, 10, 5, 2, 7];
let n2 = arr2.length;
x = 9;
let res2 = smallestSubWithSum(arr2, n2, x);
(res2 == n2 + 1)? document.write( "Not possible<br>" ) :
document.write(res2 + "<br>" );
let arr3 = [1, 11, 100, 1, 0, 200, 3, 2, 1, 250];
let n3 = arr3.length;
x = 280;
let res3 = smallestSubWithSum(arr3, n3, x);
(res3 == n3 + 1)? document.write( "Not possible<br>" ) :
document.write(res3 + "<br>" );
</script>
|
PHP
<?php
function smallestSubWithSum( $arr , $n , $x )
{
$min_len = $n + 1;
for ( $start = 0; $start < $n ; $start ++)
{
$curr_sum = $arr [ $start ];
if ( $curr_sum > $x ) return 1;
for ( $end = $start + 1; $end < $n ; $end ++)
{
$curr_sum += $arr [ $end ];
if ( $curr_sum > $x &&
( $end - $start + 1) < $min_len )
$min_len = ( $end - $start + 1);
}
}
return $min_len ;
}
$arr1 = array (1, 4, 45,
6, 10, 19);
$x = 51;
$n1 = sizeof( $arr1 );
$res1 = smallestSubWithSum( $arr1 , $n1 , $x );
if (( $res1 == $n1 + 1) == true)
echo "Not possible\n" ;
else
echo $res1 , "\n" ;
$arr2 = array (1, 10, 5, 2, 7);
$n2 = sizeof( $arr2 );
$x = 9;
$res2 = smallestSubWithSum( $arr2 , $n2 , $x );
if (( $res2 == $n2 + 1) == true)
echo "Not possible\n" ;
else
echo $res2 , "\n" ;
$arr3 = array (1, 11, 100, 1, 0,
200, 3, 2, 1, 250);
$n3 = sizeof( $arr3 );
$x = 280;
$res3 = smallestSubWithSum( $arr3 , $n3 , $x );
if (( $res3 == $n3 + 1) == true)
echo "Not possible\n" ;
else
echo $res3 , "\n" ;
?>
|
Time Complexity: O(n2).
Auxiliary Space: O(1)
Efficient Solution: This problem can be solved in O(n) time using the idea used in this post.
C++14
#include <iostream>
using namespace std;
int smallestSubWithSum( int arr[], int n, int x)
{
int curr_sum = 0, min_len = n + 1;
int start = 0, end = 0;
while (end < n) {
while (curr_sum <= x && end < n)
curr_sum += arr[end++];
while (curr_sum > x && start < n) {
if (end - start < min_len)
min_len = end - start;
curr_sum -= arr[start++];
}
}
return min_len;
}
int main()
{
int arr1[] = { 1, 4, 45, 6, 10, 19 };
int x = 51;
int n1 = sizeof (arr1) / sizeof (arr1[0]);
int res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1 + 1) ? cout << "Not possible\n"
: cout << res1 << endl;
int arr2[] = { 1, 10, 5, 2, 7 };
int n2 = sizeof (arr2) / sizeof (arr2[0]);
x = 9;
int res2 = smallestSubWithSum(arr2, n2, x);
(res2 == n2 + 1) ? cout << "Not possible\n"
: cout << res2 << endl;
int arr3[] = { 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 };
int n3 = sizeof (arr3) / sizeof (arr3[0]);
x = 280;
int res3 = smallestSubWithSum(arr3, n3, x);
(res3 == n3 + 1) ? cout << "Not possible\n"
: cout << res3 << endl;
return 0;
}
|
Java
import java.io.*;
class SmallestSubArraySum {
static int smallestSubWithSum( int arr[], int n, int x)
{
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while (end < n) {
while (curr_sum <= x && end < n)
curr_sum += arr[end++];
while (curr_sum > x && start < n) {
if (end - start < min_len)
min_len = end - start;
curr_sum -= arr[start++];
}
}
return min_len;
}
public static void main(String[] args)
{
int arr1[] = { 1 , 4 , 45 , 6 , 10 , 19 };
int x = 51 ;
int n1 = arr1.length;
int res1 = smallestSubWithSum(arr1, n1, x);
if (res1 == n1 + 1 )
System.out.println( "Not Possible" );
else
System.out.println(res1);
int arr2[] = { 1 , 10 , 5 , 2 , 7 };
int n2 = arr2.length;
x = 9 ;
int res2 = smallestSubWithSum(arr2, n2, x);
if (res2 == n2 + 1 )
System.out.println( "Not Possible" );
else
System.out.println(res2);
int arr3[]
= { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 };
int n3 = arr3.length;
x = 280 ;
int res3 = smallestSubWithSum(arr3, n3, x);
if (res3 == n3 + 1 )
System.out.println( "Not Possible" );
else
System.out.println(res3);
}
}
|
Python3
def smallestSubWithSum(arr, n, x):
curr_sum = 0
min_len = n + 1
start = 0
end = 0
while (end < n):
while (curr_sum < = x and end < n):
curr_sum + = arr[end]
end + = 1
while (curr_sum > x and start < n):
if (end - start < min_len):
min_len = end - start
curr_sum - = arr[start]
start + = 1
return min_len
arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ]
x = 51
n1 = len (arr1)
res1 = smallestSubWithSum(arr1, n1, x)
print ( "Not possible" ) if (res1 = = n1 + 1 ) else print (res1)
arr2 = [ 1 , 10 , 5 , 2 , 7 ]
n2 = len (arr2)
x = 9
res2 = smallestSubWithSum(arr2, n2, x)
print ( "Not possible" ) if (res2 = = n2 + 1 ) else print (res2)
arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ]
n3 = len (arr3)
x = 280
res3 = smallestSubWithSum(arr3, n3, x)
print ( "Not possible" ) if (res3 = = n3 + 1 ) else print (res3)
|
C#
using System;
class GFG {
static int smallestSubWithSum( int [] arr, int n, int x)
{
int curr_sum = 0, min_len = n + 1;
int start = 0, end = 0;
while (end < n) {
while (curr_sum <= x && end < n)
curr_sum += arr[end++];
while (curr_sum > x && start < n) {
if (end - start < min_len)
min_len = end - start;
curr_sum -= arr[start++];
}
}
return min_len;
}
static public void Main()
{
int [] arr1 = { 1, 4, 45, 6, 10, 19 };
int x = 51;
int n1 = arr1.Length;
int res1 = smallestSubWithSum(arr1, n1, x);
if (res1 == n1 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res1);
int [] arr2 = { 1, 10, 5, 2, 7 };
int n2 = arr2.Length;
x = 9;
int res2 = smallestSubWithSum(arr2, n2, x);
if (res2 == n2 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res2);
int [] arr3
= { 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 };
int n3 = arr3.Length;
x = 280;
int res3 = smallestSubWithSum(arr3, n3, x);
if (res3 == n3 + 1)
Console.WriteLine( "Not Possible" );
else
Console.WriteLine(res3);
}
}
|
Javascript
<script>
function smallestSubWithSum(arr, n, x)
{
let curr_sum = 0, min_len = n + 1;
let start = 0, end = 0;
while (end < n) {
while (curr_sum <= x && end < n)
curr_sum += arr[end++];
while (curr_sum > x && start < n) {
if (end - start < min_len)
min_len = end - start;
curr_sum -= arr[start++];
}
}
return min_len;
}
let arr1 = [ 1, 4, 45, 6, 10, 19 ];
let x = 51;
let n1 = arr1.length;
let res1 = smallestSubWithSum(arr1, n1, x);
(res1 == n1 + 1) ? document.write( "Not possible<br>" )
: document.write(res1 + "<br>" );
let arr2 = [ 1, 10, 5, 2, 7 ];
let n2 = arr2.length;
x = 9;
let res2 = smallestSubWithSum(arr2, n2, x);
(res2 == n2 + 1) ? document.write( "Not possible<br>" )
: document.write(res2 + "<br>" );
let arr3 = [ 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 ];
let n3 = arr3.length;
x = 280;
let res3 = smallestSubWithSum(arr3, n3, x);
(res3 == n3 + 1) ? document.write( "Not possible<br>" )
: document.write(res3 + "<br>" );
</script>
|
PHP
<?php
function smallestSubWithSum( $arr ,
$n , $x )
{
$curr_sum = 0;
$min_len = $n + 1;
$start = 0;
$end = 0;
while ( $end < $n )
{
while ( $curr_sum <= $x &&
$end < $n )
$curr_sum += $arr [ $end ++];
while ( $curr_sum > $x &&
$start < $n )
{
if ( $end - $start < $min_len )
$min_len = $end - $start ;
$curr_sum -= $arr [ $start ++];
}
}
return $min_len ;
}
$arr1 = array (1, 4, 45,
6, 10, 19);
$x = 51;
$n1 = sizeof( $arr1 );
$res1 = smallestSubWithSum( $arr1 ,
$n1 , $x );
if ( $res1 == $n1 + 1)
echo "Not possible\n" ;
else
echo $res1 , "\n" ;
$arr2 = array (1, 10, 5, 2, 7);
$n2 = sizeof( $arr2 );
$x = 9;
$res2 = smallestSubWithSum( $arr2 ,
$n2 , $x );
if ( $res2 == $n2 + 1)
echo "Not possible\n" ;
else
echo $res2 , "\n" ;
$arr3 = array (1, 11, 100, 1, 0,
200, 3, 2, 1, 250);
$n3 = sizeof( $arr3 );
$x = 280;
$res3 = smallestSubWithSum( $arr3 ,
$n3 , $x );
if ( $res3 == $n3 + 1)
echo "Not possible\n" ;
else
echo $res3 , "\n" ;
?>
|
Time Complexity: O(n).
Auxiliary Space: O(1)
Another Approach: Binary Search
- First calculates the cumulative sum of the vector elements and stores them in the sums vector.
- Then iterates through the sums vector and finds the lower bound of the target sum for each possible subarray.
- If the lower bound is found and it’s not equal to the target sum (i.e., the subarray sum is greater than the target),
- Calculates the length of the subarray and updates the ans variable if the length is smaller than the current value.
- Finally, returns the ans value or 0 if ans was not updated.
C++
#include <bits/stdc++.h>
using namespace std;
int smallestSubArrayLen( int target, vector< int >& nums)
{
int n = nums.size();
if (n == 0)
return 0;
int ans = INT_MAX - 1;
vector< int > sums(n + 1, 0);
for ( int i = 1; i <= n; i++)
sums[i] = sums[i - 1] + nums[i - 1];
for ( int i = 1; i <= n; i++) {
int to_find = target + sums[i - 1];
auto bound = lower_bound(sums.begin(), sums.end(),
to_find);
if (bound != sums.end() && *bound != to_find) {
int len = bound - (sums.begin() + i - 1);
ans = min(ans, len);
}
}
return (ans != INT_MAX - 1) ? ans : 0;
}
int main()
{
vector< int > arr1 = { 1, 4, 45, 6, 10, 19 };
int target1 = 51;
cout << "Length of Smallest Subarray :"
<< smallestSubArrayLen(target1, arr1) << endl;
vector< int > arr2 = { 1, 10, 5, 2, 7 };
int target2 = 9;
cout << "Length of Smallest Subarray :"
<< smallestSubArrayLen(target2, arr2) << endl;
vector< int > arr3 = { 1, 1, 1, 1, 1, 1, 1, 1 };
int target3 = 11;
cout << "Length of Smallest Subarray :"
<< smallestSubArrayLen(target3, arr3) << endl;
vector< int > arr4
= { 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 };
int target4 = 280;
cout << "Length of Smallest Subarray :"
<< smallestSubArrayLen(target4, arr4) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int smallestSubArrayLen( int target, int [] nums) {
int n = nums.length;
if (n == 0 ) {
return 0 ;
}
int ans = Integer.MAX_VALUE;
int [] sums = new int [n + 1 ];
for ( int i = 1 ; i <= n; i++) {
sums[i] = sums[i - 1 ] + nums[i - 1 ];
}
for ( int i = 1 ; i <= n; i++) {
int toFind = target + sums[i - 1 ];
int bound = Arrays.binarySearch(sums, toFind);
if (bound < 0 ) {
bound = -bound - 1 ;
}
if (bound != sums.length && sums[bound] != toFind) {
int length = bound - (i - 1 );
ans = Math.min(ans, length);
}
}
return (ans != Integer.MAX_VALUE) ? ans : 0 ;
}
public static void main(String[] args) {
int [] arr1 = { 1 , 4 , 45 , 6 , 10 , 19 };
int target1 = 51 ;
System.out.println( "Length of Smallest Subarray: " + smallestSubArrayLen(target1, arr1));
int [] arr2 = { 1 , 10 , 5 , 2 , 7 };
int target2 = 9 ;
System.out.println( "Length of Smallest Subarray: " + smallestSubArrayLen(target2, arr2));
int [] arr3 = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
int target3 = 11 ;
System.out.println( "Length of Smallest Subarray: " + smallestSubArrayLen(target3, arr3));
int [] arr4 = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 };
int target4 = 280 ;
System.out.println( "Length of Smallest Subarray: " + smallestSubArrayLen(target4, arr4));
}
}
|
Python3
from bisect import bisect_left
def smallestSubArrayLen(target, nums):
n = len (nums)
if n = = 0 :
return 0
ans = float ( 'inf' )
sums = [ 0 ] * (n + 1 )
for i in range ( 1 , n + 1 ):
sums[i] = sums[i - 1 ] + nums[i - 1 ]
for i in range ( 1 , n + 1 ):
to_find = target + sums[i - 1 ]
bound = bisect_left(sums, to_find)
if bound ! = len (sums) and sums[bound] ! = to_find:
length = bound - (i - 1 )
ans = min (ans, length)
return ans if ans ! = float ( 'inf' ) else 0
arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ]
target1 = 51
print ( "Length of Smallest Subarray:" , smallestSubArrayLen(target1, arr1))
arr2 = [ 1 , 10 , 5 , 2 , 7 ]
target2 = 9
print ( "Length of Smallest Subarray:" , smallestSubArrayLen(target2, arr2))
arr3 = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]
target3 = 11
print ( "Length of Smallest Subarray:" , smallestSubArrayLen(target3, arr3))
arr4 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ]
target4 = 280
print ( "Length of Smallest Subarray:" , smallestSubArrayLen(target4, arr4))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int SmallestSubArrayLen( int target, List< int > nums)
{
int n = nums.Count;
if (n == 0)
return 0;
int ans = int .MaxValue - 1;
List< int > sums = new List< int >(n + 1);
sums.Add(0);
for ( int i = 1; i <= n; i++)
sums.Add(sums[i - 1] + nums[i - 1]);
for ( int i = 1; i <= n; i++)
{
int toFind = target + sums[i - 1];
int bound = BinarySearch(sums, toFind);
if (bound != sums.Count && sums[bound] != toFind)
{
int len = bound - (i - 1);
ans = Math.Min(ans, len);
}
}
return (ans != int .MaxValue - 1) ? ans : 0;
}
static int BinarySearch(List< int > sums, int target)
{
int left = 0;
int right = sums.Count - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (sums[mid] == target)
return mid;
else if (sums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
static void Main()
{
List< int > arr1 = new List< int > { 1, 4, 45, 6, 10, 19 };
int target1 = 51;
Console.WriteLine( "Length of Smallest Subarray: " + SmallestSubArrayLen(target1, arr1));
List< int > arr2 = new List< int > { 1, 10, 5, 2, 7 };
int target2 = 9;
Console.WriteLine( "Length of Smallest Subarray: " + SmallestSubArrayLen(target2, arr2));
List< int > arr3 = new List< int > { 1, 1, 1, 1, 1, 1, 1, 1 };
int target3 = 11;
Console.WriteLine( "Length of Smallest Subarray: " + SmallestSubArrayLen(target3, arr3));
List< int > arr4 = new List< int > { 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 };
int target4 = 280;
Console.WriteLine( "Length of Smallest Subarray: " + SmallestSubArrayLen(target4, arr4));
}
}
|
Javascript
function smallestSubArrayLen(target, nums) {
let n = nums.length;
if (n === 0)
return 0;
let ans = Infinity;
let sums = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++)
sums[i] = sums[i - 1] + nums[i - 1];
for (let i = 1; i <= n; i++) {
let to_find = target + sums[i - 1];
let bound = sums.findIndex((element) => element >= to_find);
if (bound !== -1 && sums[bound] !== to_find) {
let len = bound - (i - 1);
ans = Math.min(ans, len);
}
}
return (ans !== Infinity) ? ans : 0;
}
let arr1 = [1, 4, 45, 6, 10, 19];
let target1 = 51;
console.log( "Length of Smallest Subarray: " + smallestSubArrayLen(target1, arr1));
let arr2 = [1, 10, 5, 2, 7];
let target2 = 9;
console.log( "Length of Smallest Subarray: " + smallestSubArrayLen(target2, arr2));
let arr3 = [1, 1, 1, 1, 1, 1, 1, 1];
let target3 = 11;
console.log( "Length of Smallest Subarray: " + smallestSubArrayLen(target3, arr3));
let arr4 = [1, 11, 100, 1, 0, 200, 3, 2, 1, 250];
let target4 = 280;
console.log( "Length of Smallest Subarray: " + smallestSubArrayLen(target4, arr4));
|
Output
Length of Smallest Subarray :3
Length of Smallest Subarray :1
Length of Smallest Subarray :0
Length of Smallest Subarray :4
Time Complexity: O (n log(n)).
Auxiliary Space: O(n)
Thanks to Ankit and Nitin for suggesting this optimized solution.
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
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!