The Wayback Machine - https://web.archive.org/web/20240818152008/https://www.geeksforgeeks.org/smallest-sum-contiguous-subarray/
Open In App

Smallest sum contiguous subarray

Last Updated : 27 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array containing n integers. The problem is to find the sum of the elements of the contiguous subarray having the smallest(minimum) sum.

Examples: 

Input : arr[] = {3, -4, 2, -3, -1, 7, -5}
Output : -6
Subarray is {-4, 2, -3, -1} = -6
Input : arr = {2, 6, 8, 1, 4}
Output : 1

Naive Approach: Consider all the contiguous subarrays of different sizes and find their sum. The subarray having the smallest(minimum) sum is the required answer.

Efficient Approach: It is a variation to the problem of finding the largest sum contiguous subarray based on the idea of Kadane’s algorithm. 

Algorithm: 

smallestSumSubarr(arr, n)
Initialize min_ending_here = INT_MAX
Initialize min_so_far = INT_MAX

for i = 0 to n-1
if min_ending_here > 0
min_ending_here = arr[i]
else
min_ending_here += arr[i]
min_so_far = min(min_so_far, min_ending_here)
return min_so_far

Below is the implementation of the above approach:

C++




// C++ implementation to find the smallest sum
// contiguous subarray
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the smallest sum contiguous subarray
int smallestSumSubarr(int arr[], int n)
{
    // to store the minimum value that is ending
    // up to the current index
    int min_ending_here = INT_MAX;
     
    // to store the minimum value encountered so far
    int min_so_far = INT_MAX;
     
    // traverse the array elements
    for (int i=0; i<n; i++)
    {
        // if min_ending_here > 0, then it could not possibly
        // contribute to the minimum sum further
        if (min_ending_here > 0)
            min_ending_here = arr[i];
         
        // else add the value arr[i] to min_ending_here   
        else
            min_ending_here += arr[i];
         
        // update min_so_far
        min_so_far = min(min_so_far, min_ending_here);           
    }
     
    // required smallest sum contiguous subarray value
    return min_so_far;
}
 
 
// Driver program to test above
int main()
{
    int arr[] = {3, -4, 2, -3, -1, 7, -5};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Smallest sum: "
         << smallestSumSubarr(arr, n);
    return 0;    
}


Java




// Java implementation to find the smallest sum
// contiguous subarray
import java.io.*;
class GFG {
     
    // function to find the smallest sum contiguous
    // subarray
    static int smallestSumSubarr(int arr[], int n)
    {
         
        // to store the minimum value that is
        // ending up to the current index
        int min_ending_here = 2147483647;
         
        // to store the minimum value encountered
        // so far
        int min_so_far = 2147483647;
         
        // traverse the array elements
        for (int i = 0; i < n; i++)
        {
             
            // if min_ending_here > 0, then it could
            // not possibly contribute to the
            // minimum sum further
            if (min_ending_here > 0)
                min_ending_here = arr[i];
             
            // else add the value arr[i] to
            // min_ending_here
            else
                min_ending_here += arr[i];
             
            // update min_so_far
            min_so_far = Math.min(min_so_far,
                                   min_ending_here);        
        }
         
        // required smallest sum contiguous
        // subarray value
        return min_so_far;
    }
     
    // Driver method
    public static void main(String[] args)
    {
         
        int arr[] = {3, -4, 2, -3, -1, 7, -5};
        int n = arr.length;
         
        System.out.print("Smallest sum: "
                + smallestSumSubarr(arr, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to find the smallest sum
# contiguous subarray
maxsize=int(1e9+7)
 
# function to find the smallest sum
# contiguous subarray
def smallestSumSubarr(arr, n):
    # to store the minimum value that is ending
    # up to the current index
    min_ending_here = maxsize
     
    # to store the minimum value encountered so far
    min_so_far = maxsize
     
    # traverse the array elements
    for i in range(n):
        # if min_ending_here > 0, then it could not possibly
        # contribute to the minimum sum further
        if (min_ending_here > 0):
            min_ending_here = arr[i]
         
        # else add the value arr[i] to min_ending_here
        else:
            min_ending_here += arr[i]
          
        # update min_so_far
        min_so_far = min(min_so_far, min_ending_here)
     
    # required smallest sum contiguous subarray value
    return min_so_far
     
# Driver code
arr = [3, -4, 2, -3, -1, 7, -5]
n = len(arr)
print ("Smallest sum: ", smallestSumSubarr(arr, n))
 
# This code is contributed by Sachin Bisht


C#




// C# implementation to find the
// smallest sum contiguous subarray
using System;
 
class GFG {
 
    // function to find the smallest sum
    // contiguous subarray
    static int smallestSumSubarr(int[] arr, int n)
    {
        // to store the minimum value that is
        // ending up to the current index
        int min_ending_here = 2147483647;
 
        // to store the minimum value encountered
        // so far
        int min_so_far = 2147483647;
 
        // traverse the array elements
        for (int i = 0; i < n; i++) {
 
            // if min_ending_here > 0, then it could
            // not possibly contribute to the
            // minimum sum further
            if (min_ending_here > 0)
                min_ending_here = arr[i];
 
            // else add the value arr[i] to
            // min_ending_here
            else
                min_ending_here += arr[i];
 
            // update min_so_far
            min_so_far = Math.Min(min_so_far,
                                min_ending_here);
        }
 
        // required smallest sum contiguous
        // subarray value
        return min_so_far;
    }
 
    // Driver method
    public static void Main()
    {
 
        int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
        int n = arr.Length;
 
        Console.Write("Smallest sum: " +
             smallestSumSubarr(arr, n));
    }
}
 
// This code is contributed by Sam007


Javascript




<script>
 
    // JavaScript implementation to find the
    // smallest sum contiguous subarray
     
    // function to find the smallest sum
    // contiguous subarray
    function smallestSumSubarr(arr, n)
    {
        // to store the minimum value that is
        // ending up to the current index
        let min_ending_here = 2147483647;
   
        // to store the minimum value encountered
        // so far
        let min_so_far = 2147483647;
   
        // traverse the array elements
        for (let i = 0; i < n; i++) {
   
            // if min_ending_here > 0, then it could
            // not possibly contribute to the
            // minimum sum further
            if (min_ending_here > 0)
                min_ending_here = arr[i];
   
            // else add the value arr[i] to
            // min_ending_here
            else
                min_ending_here += arr[i];
   
            // update min_so_far
            min_so_far = Math.min(min_so_far,
                                min_ending_here);
        }
   
        // required smallest sum contiguous
        // subarray value
        return min_so_far;
    }
     
    let arr = [ 3, -4, 2, -3, -1, 7, -5 ];
    let n = arr.length;
 
    document.write("Smallest sum: " +
                  smallestSumSubarr(arr, n));
     
</script>


PHP




<?php
// PHP implementation to find the
// smallest sum contiguous subarray
 
// function to find the smallest
// sum contiguous subarray
function smallestSumSubarr($arr, $n)
{
     
    // to store the minimum
    // value that is ending
    // up to the current index
    $min_ending_here = 999999;
     
    // to store the minimum value
    // encountered so far
    $min_so_far = 999999;
     
    // traverse the array elements
    for($i = 0; $i < $n; $i++)
    {
         
        // if min_ending_here > 0,
        // then it could not possibly
        // contribute to the minimum
        // sum further
        if ($min_ending_here > 0)
            $min_ending_here = $arr[$i];
         
        // else add the value arr[i]
        // to min_ending_here
        else
            $min_ending_here += $arr[$i];
         
        // update min_so_far
        $min_so_far = min($min_so_far,
                     $min_ending_here);        
    }
     
    // required smallest sum
    // contiguous subarray value
    return $min_so_far;
}
 
 
    // Driver Code
    $arr = array(3, -4, 2, -3, -1, 7, -5);
    $n = count($arr) ;
    echo "Smallest sum: "
         .smallestSumSubarr($arr, $n);
 
// This code is contributed by Sam007
?>


Output

Smallest sum: -6










Time Complexity: O(n)
Auxiliary Space: O(1)

 

New Approach:- Here, Another approach to solve this problem is to use a prefix sum array. The prefix sum array is an auxiliary array that stores the sum of all the elements up to a certain index in the original array. We can use this prefix sum array to find the smallest sum contiguous subarray by finding the minimum difference between two prefix sum elements.

Algorithm:

smallestSumSubarr(arr, n)
Initialize prefixSum array with 0 at index 0
for i = 1 to n
prefixSum[i] = prefixSum[i-1] + arr[i-1]
Initialize min_sum = INT_MAX
for i = 0 to n-1
for j = i+1 to n
min_sum = min(min_sum, prefixSum[j] - prefixSum[i])
return min_sum

Below is the implementation of the above approach:

C++




// C++ implementation to find the smallest sum
// contiguous subarray using prefix sum array
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the smallest sum contiguous subarray
int smallestSumSubarr(int arr[], int n)
{
    int prefixSum[n + 1];
    prefixSum[0] = 0;
    for (int i = 1; i <= n; i++) {
        prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
    }
    int min_sum = INT_MAX;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
            min_sum
                = min(min_sum, prefixSum[j] - prefixSum[i]);
        }
    }
 
    // required smallest sum contiguous subarray value
    return min_sum;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 3, -4, 2, -3, -1, 7, -5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Smallest sum: " << smallestSumSubarr(arr, n);
    return 0;
}


Java




// Java implementation to find the smallest sum
// contiguous subarray using prefix sum array
import java.util.Arrays;
 
public class Main {
 
    // function to find the smallest sum contiguous subarray
    public static int smallestSumSubarr(int[] arr, int n) {
        int[] prefixSum = new int[n + 1];
        prefixSum[0] = 0;
        for (int i = 1; i <= n; i++) {
            prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
        }
        int min_sum = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                min_sum = Math.min(min_sum, prefixSum[j] - prefixSum[i]);
            }
        }
 
        // required smallest sum contiguous subarray value
        return min_sum;
    }
 
    // Driver program to test above
    public static void main(String[] args) {
        int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
        int n = arr.length;
        System.out.println("Smallest sum: " + smallestSumSubarr(arr, n));
    }
}
 
// This code is contributed by Utkarsh Kumar


Python3




# function to find the smallest sum contiguous subarray
def smallestSumSubarr(arr, n):
    prefixSum = [0] * (n + 1)
    prefixSum[0] = 0
 
    for i in range(1, n + 1):
        prefixSum[i] = prefixSum[i - 1] + arr[i - 1]
 
    min_sum = float('inf')
 
    for i in range(n):
        for j in range(i + 1, n + 1):
            min_sum = min(min_sum, prefixSum[j] - prefixSum[i])
 
    # required smallest sum contiguous subarray value
    return min_sum
 
 
# Driver program to test above
arr = [3, -4, 2, -3, -1, 7, -5]
n = len(arr)
print("Smallest sum:", smallestSumSubarr(arr, n))


C#




// C# implementation to find the
// smallest sum contiguous subarray
using System;
 
class GFG {
 
    // function to find the smallest sum
    // contiguous subarray
    static int smallestSumSubarr(int[] arr, int n)
    {
        int[] prefixSum=new int[n+1];
        prefixSum[0] = 0;
        for (int i = 1; i <= n; i++) {
            prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
        }
        int min_sum = 2147483647;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                min_sum
                    = Math.Min(min_sum, prefixSum[j] - prefixSum[i]);
            }
        }
 
        // required smallest sum contiguous subarray value
        return min_sum;
    }
 
    // Driver method
    public static void Main()
    {
 
        int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
        int n = arr.Length;
 
        Console.Write("Smallest sum: " +
             smallestSumSubarr(arr, n));
    }
}
 
// This code is contributed by shubhamrajput6156


Javascript




// Javascript implementation to find the smallest sum
// contiguous subarray using prefix sum array
 
// Function to find the smallest sum contiguous subarray
function smallestSumSubarr(arr, n) {
    // Create a prefix sum array
    let prefixSum = new Array(n + 1);
    prefixSum[0] = 0;
 
    // Calculate prefix sum
    for (let i = 1; i <= n; i++) {
        prefixSum[i] = prefixSum[i - 1] + arr[i - 1];
    }
 
    let min_sum = Infinity;
 
    // Find the minimum sum subarray
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j <= n; j++) {
            min_sum = Math.min(min_sum, prefixSum[j] - prefixSum[i]);
        }
    }
 
    // Return the smallest sum contiguous subarray value
    return min_sum;
}
 
// Driver code to test the function
let arr = [3, -4, 2, -3, -1, 7, -5];
let n = arr.length;
console.log("Smallest sum: " + smallestSumSubarr(arr, n));
 
// THIS CODE IS CONTRIBUTED BY YASH AGARWAL(YASHAGARWAL2852002)


Output:-

Smallest sum: -6

Time Complexity: O(n^2)
Auxiliary Space: O(n) 



Similar Reads

Smallest sum contiguous subarray | Set-2
Given an array containing N integers. The task is to find the sum of the elements of the contiguous subarray having the smallest(minimum) sum.Examples: Input: arr[] = {3, -4, 2, -3, -1, 7, -5} Output:-6 Input: arr = {2, 6, 8, 1, 4} Output: 1 An approach has already been discussed in the previous post. In this post, a solution using the approach of
5 min read
Largest Sum Contiguous Subarray having unique elements
Given an array arr[] of N positive integers, the task is to find the subarray having maximum sum among all subarrays having unique elements and print its sum. Input arr[] = {1, 2, 3, 3, 4, 5, 2, 1}Output: 15Explanation:The subarray having maximum sum with distinct element is {3, 4, 5, 2, 1}.Therefore, sum is = 3 + 4 + 5 + 2 + 1 = 15 Input: arr[] =
15+ min read
Python3 Program to Find the K-th Largest Sum Contiguous Subarray
Given an array of integers. Write a program to find the K-th largest sum of contiguous subarray within the array of numbers which has negative and positive numbers. Examples:  Input: a[] = {20, -5, -1} k = 3 Output: 14 Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1) so the 3rd largest sum is 14. Input: a[] = {10, -10, 20,
3 min read
Largest sum contiguous subarray by adding S exactly at K different positions
Given an array arr[] of length N, the task is to find the largest sum contiguous subarray by adding an integer S exactly at K different positions in the array for every K from [0, N]. Examples: Input: arr[] = {4, 1, 3, 2}, S = 2Output: 10 12 14 16 18Explanation:For k = 0, the sum of the array it self is the maximum subarray sum since no negative el
10 min read
Range query for Largest Sum Contiguous Subarray
Given an array a[] of size N, and Q queries of two types 1 and 2. For the given query of: Type 1: given l and r (they are positions), and task is to print the Largest sum Contiguous Subarray Type 2: given type, index, and value, update aindex = value. Examples: Input: a[] = {-2, -3, 4, -1, -2, 1, 5, -3} 1st query : 1 5 8 2nd query : 2 1 10 3rd quer
15+ min read
Largest sum contiguous subarray having only non-negative elements
Given an integer array arr[], the task is to find the largest sum contiguous subarray of non-negative elements and return its sum. Examples: Input: arr[] = {1, 4, -3, 9, 5, -6} Output: 14 Explanation: Subarray [9, 5] is the subarray having maximum sum with all non-negative elements. Input: arr[] = {12, 0, 10, 3, 11} Output: 36 Recommended PracticeM
8 min read
Javascript Program for Largest Sum Contiguous Subarray
Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Kadane's Algorithm: Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = m
5 min read
Largest sum contiguous increasing subarray
Given an array of n positive distinct integers. The problem is to find the largest sum of contiguous increasing subarray in O(n) time complexity. Examples : Input : arr[] = {2, 1, 4, 7, 3, 6}Output : 12Contiguous Increasing subarray {1, 4, 7} = 12Input : arr[] = {38, 7, 8, 10, 12}Output : 38Recommended PracticeGreedy FoxTry It!A simple solution is
15 min read
K-th Largest Sum Contiguous Subarray
Given an array of integers. Write a program to find the K-th largest sum of contiguous subarray within the array of numbers that has both negative and positive numbers. Examples: Input: a[] = {20, -5, -1}, K = 3Output: 14Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1) so the 3rd largest sum is 14. Input: a[] = {10, -10, 20
15+ min read
Find the smallest contiguous sum pair in an Array
Given an array arr[] containing N distinct integers, the task is to find a contiguous pair such that the sum of both elements in the pair is minimum. Examples: Input: arr[] = {1, 2, 3, 4} Output: (1, 2) Explanation: Here, contiguous pairs with their sum are (1, 2) = 3, (2, 3) = 5, (3, 4) = 7 and the minimum is 3.Input: arr[] = {4, 9, -3, 2, 0} Outp
5 min read
Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
Given an array arr[] of length N, the task is to find the smallest pair of indices (i, j) such that the product of elements in the subarray arr[i + 1, j - 1] is co-prime with either the product of the subarray arr[0, i] or that of the subarray arr[j, N]. If no such pair exists, the print "-1". Examples: Input: arr[] = {2, 4, 1, 3, 7}Output: (0, 2)E
12 min read
Length of the largest subarray with contiguous elements | Set 1
Given an array of distinct integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence. Examples: Input: arr[] = {10, 12, 11}; Output: Length of the longest contiguous subarray is 3 Input: arr[] = {14, 12, 11, 20}; Output: Length of the longest contiguous subarray is 2 Input: arr[] = {1, 56, 58
7 min read
Length of the largest subarray with contiguous elements | Set 2
Given an array of integers, find length of the longest subarray which contains numbers that can be arranged in a continuous sequence. In the previous post, we have discussed a solution that assumes that elements in given array are distinct. Here we discuss a solution that works even if the input array has duplicates. Examples: Input: arr[] = {10, 1
7 min read
Largest Ratio Contiguous subarray
Given an array arr[] of N numbers, the task is to find the largest ratio of contiguous subarray from the given array. Examples: Input: arr = { -1, 10, 0.1, -8, -2 }Output: 100 Explanation:The subarray {10, 0.1} gives 10 / 0.1 = 100 which is the largest ratio. Input: arr = { 2, 2, 4, -0.2, -1 }Output: 20Explanation:The subarray {4, -0.2, -1} has the
5 min read
Check if a non-contiguous subsequence same as the given subarray exists or not
Given an array arr[] consisting of N integers and two integer values L and R, indicating the starting and ending indices of a subarray, the task is to check if there exists a non-contiguous subsequence which is same as the given subarray or not. If found to be true, print "Yes". Otherwise, print "No". A non-contiguous subsequence contains at least
7 min read
Maximum length of longest increasing contiguous subarray after deleting exactly one element from array
Given an array arr[] of N integers. The task is to find the maximum length of the longest increasing contiguous subarray after removing exactly one element from the given array. Examples : Input : N = 5, arr[] = { 2, 4, 1, 5, 7 }Output : 4Explanation : After removing third element from the array, the array arr[ ] becomes [2, 4, 5, 7] . therefore, t
15+ min read
First subarray having sum at least half the maximum sum of any subarray of size K
Given an array arr[] and an integer K, the task is to find the first subarray which has a sum greater than or equal to half of the maximum possible sum from any subarray of size K. Examples: Input: arr[] = {2, 4, 5, 1, 4, 6, 6, 2, 1, 0}, K = 3 Output: 6 2 1 Explanation: The given array has a maximum possible sum from any subarray of size K is 16 fr
9 min read
Minimum cost to convert all elements of a K-size subarray to 0 from given Ternary Array with subarray sum as cost
Given an array arr[] of N integers, where each array element is either 0, 1, or 2, and an integer K, the task is to print the minimum cost needed to convert all the elements of the array to 0s by selecting a subarray of size K and converting any array element of the subarray to 0 in one operation with the cost equal to the sum of elements of the su
11 min read
Maximize the subarray sum after multiplying all elements of any subarray with X
Given an array arr[] of N integers and an integer X. We can choose any sub-array and multiply all its element by X. After multiplication, find the sub-array with the maximum sum. The task is to multiply the sub-array in such a way that the final sub-array sum is maximized. Examples: Input: arr[] = { -3, 8, -2, 1, -6 }, X = -1 Output: 15 Choose the
15+ min read
Find minimum subarray sum for each index i in subarray [i, N-1]
Given an array arr[] of size N, the task is to find the minimum subarray sum in the subarrays [i, N-1] for all i in [0, N-1]. Examples: Input: arr[ ] = {3, -1, -2}Output: -3 -3 -2Explanation: For (i = 1) i.e. {3, -1, -2}, the minimum subarray sum is -3 for {-1, -2}.For (i = 2) i.e. {-1, -2}, the minimum subarray sum is -3 for {-1, -2}.For (i = 3) i
9 min read
Maximize subarray sum by inverting sign of elements of any subarray at most twice
Given an array A of size n, find the maximum subarray sum after applying the given operation at most two times. In one operation, choose any two indices i and j and invert sign of all the elements from index i to index j, i.e, all positive elements in the range i to j become negative and all negative elements become positive. Examples: Input: A[] =
10 min read
Maximize product of min value of subarray and sum of subarray over all subarrays of length K
Given an array arr[] of N integers, the task is to find the maximum possible value of (min * sum) among all possible subarrays having K elements, where min denotes the smallest integer of the subarray and sum denotes the sum of all elements of the subarray. Example: Input: arr[] = {1, 2, 3, 2}, K = 3Output: 14Explanation: For the subarray {2, 3, 2}
7 min read
Count of subarray that does not contain any subarray with sum 0
Given an array arr, the task is to find the total number of subarrays of the given array which do not contain any subarray whose sum of elements is equal to zero. All the array elements may not be distinct.Examples: Input: arr = {2, 4, -6} Output: 5 Explanation: There are 5 subarrays which do not contain any subarray whose elements sum is equal to
11 min read
Length of Smallest subarray in range 1 to N with sum greater than a given value
Given two numbers N and S, the task is to find the length of smallest subarray in range (1, N) such that the sum of those chosen numbers is greater than S. Examples: Input: N = 5, S = 11 Output: 3 Explanation: Smallest subarray with sum &gt; 11 = {5, 4, 3} Input: N = 4, S = 7 Output: 3 Explanation: Smallest subarray with sum &gt; 7 = {4, 3, 2} Naiv
9 min read
Smallest Subarray with Sum K from an Array
Given an array arr[] consisting of N integers, the task is to find the length of the Smallest subarray with a sum equal to K. Examples: Input: arr[] = {2, 4, 6, 10, 2, 1}, K = 12 Output: 2 Explanation: All possible subarrays with sum 12 are {2, 4, 6} and {10, 2}. Input: arr[] = {-8, -8, -3, 8}, K = 5 Output: 2 Naive Approach: The simplest approach
10 min read
Smallest subarray from a given Array with sum greater than or equal to K | Set 2
Given an array A[] consisting of N positive integers and an integer K, the task is to find the length of the smallest subarray with a sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: arr[] = {3, 1, 7, 1, 2}, K = 11Output: 3Explanation:The smallest subarray with sum ? K(= 11) is {3, 1, 7}. Input: arr[] = {2, 3,
15+ min read
Smallest subarray of size greater than K with sum greater than a given value
Given an array, arr[] of size N, two positive integers K and S, the task is to find the length of the smallest subarray of size greater than K, whose sum is greater than S. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 1, S = 8Output: 2Explanation: Smallest subarray with sum greater than S(=8) is {4, 5}Therefore, the required output is 2. Input: ar
15 min read
Length of smallest subarray to be removed to make sum of remaining elements divisible by K
Given an array arr[] of integers and an integer K, the task is to find the length of the smallest subarray that needs to be removed such that the sum of remaining array elements is divisible by K. Removal of the entire array is not allowed. If it is impossible, then print "-1". Examples: Input: arr[] = {3, 1, 4, 2}, K = 6Output: 1Explanation: Sum o
11 min read
Length of the smallest subarray with maximum possible sum
Given an array arr[] consisting of N non-negative integers, the task is to find the minimum length of the subarray whose sum is maximum. Example: Input: arr[] = {0, 2, 0, 0, 12, 0, 0, 0}Output: 4Explanation: The sum of the subarray {2, 0, 0, 12} = 2 + 0 + 0 + 12 = 14, which is maximum sum possible and the length of the subarray is 4, which is minim
6 min read
Smallest subarray with sum greater than or equal to K
Given an array A[] consisting of N integers and an integer K, the task is to find the length of the smallest subarray with sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: A[] = {2, -1, 2}, K = 3 Output: 3 Explanation: Sum of the given array is 3. Hence, the smallest possible subarray satisfying the required co
15+ min read
three90RightbarBannerImg