Smallest sum contiguous subarray
Last Updated :
07 Mar, 2025
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
Explanation: Subarray with minimum sum is {-4, 2, -3, -1} = -6
Input: arr = {2, 6, 8, 1, 4}
Output: 1
Explanation: Subarray with minimum sum is {1} = 1
[Naive Approach] - Using Nested Loops to Find all Subarray and their Sum - O(n^2) Time and O(1) Space
Consider all the contiguous subarrays of different sizes and find their sum. The subarray having the smallest(minimum) sum is the required answer.
C++
#include <bits/stdc++.h>
using namespace std;
int smallestSumSubarr(vector<int> arr)
{
int n = arr.size();
int min_sum = INT_MAX;
int temp = 0;
for( int i=0; i<n; i++)
{
temp =0;
for( int j=i; j<n; j++)
{
temp+= arr[j];
min_sum = min(min_sum, temp);
}
}
return min_sum;
}
int main()
{
vector<int> arr = {3, -4, 2, -3, -1, 7, -5};
cout <<smallestSumSubarr(arr);
return 0;
}
Java
import java.util.Arrays;
import java.util.List;
public class Main {
public static int smallestSumSubarr(List<Integer> arr) {
int n = arr.size();
int min_sum = Integer.MAX_VALUE;
int temp;
for (int i = 0; i < n; i++) {
temp = 0;
for (int j = i; j < n; j++) {
temp += arr.get(j);
min_sum = Math.min(min_sum, temp);
}
}
return min_sum;
}
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(3, -4, 2, -3, -1, 7, -5);
System.out.println(smallestSumSubarr(arr));
}
}
Python
def smallest_sum_subarr(arr):
n = len(arr)
min_sum = float('inf')
for i in range(n):
temp = 0
for j in range(i, n):
temp += arr[j]
min_sum = min(min_sum, temp)
return min_sum
arr = [3, -4, 2, -3, -1, 7, -5]
print(smallest_sum_subarr(arr))
C#
using System;
using System.Collections.Generic;
class Program {
static int SmallestSumSubarr(List<int> arr) {
int n = arr.Count;
int min_sum = int.MaxValue;
int temp;
for (int i = 0; i < n; i++) {
temp = 0;
for (int j = i; j < n; j++) {
temp += arr[j];
min_sum = Math.Min(min_sum, temp);
}
}
return min_sum;
}
static void Main() {
List<int> arr = new List<int> { 3, -4, 2, -3, -1, 7, -5 };
Console.WriteLine(SmallestSumSubarr(arr));
}
}
JavaScript
function smallestSumSubarr(arr) {
let n = arr.length;
let min_sum = Infinity;
for (let i = 0; i < n; i++) {
let temp = 0;
for (let j = i; j < n; j++) {
temp += arr[j];
min_sum = Math.min(min_sum, temp);
}
}
return min_sum;
}
const arr = [3, -4, 2, -3, -1, 7, -5];
console.log(smallestSumSubarr(arr));
[Expected Approach] - Using Kadane's Algirithm - O(n) Time and O(1) Space
It is a variation to the problem of finding the largest sum contiguous subarray based on the idea of Kadane’s algorithm.
The idea is to traverse over the array from left to right and for each element, find the minimum sum sum among all subarrays ending at that element. The result will be the maximum of all these values.
But, the main issue is how to calculate minimum sum among all the subarrays ending at an element in O(n) time?
To calculate the minimum sum of subarray ending at current element, say min_ending_here, we can use the minimum sum ending at the previous element. So for any element, we have two choices:
- Choice 1: Extend the minimum sum subarray ending at the previous element by adding the current element to it. If the minimum subarray sum ending at the previous index is negative, then it is always better to extend the subarray.
- Choice 2: Start a new subarray starting from the current element. If the minimum subarray sum ending at the previous index is positive, it is always better to start a new subarray from the current element.
C++
#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;
}
int main()
{
int arr[] = {3, -4, 2, -3, -1, 7, -5};
int n = sizeof(arr) / sizeof(arr[0]);
cout <<smallestSumSubarr(arr, n);
return 0;
}
Java
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;
}
public static void main(String[] args)
{
int arr[] = { 3, -4, 2, -3, -1, 7, -5 };
int n = arr.length;
System.out.print(smallestSumSubarr(arr, n));
}
}
// This code is contributed by Anant Agarwal.
Python
maxsize = int(1e9 + 7)
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
arr = [3, -4, 2, -3, -1, 7, -5]
n = len(arr)
print(smallestSumSubarr(arr, n))
C#
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;
}
public static void Main()
{
int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
int n = arr.Length;
Console.Write(smallestSumSubarr(arr, n));
}
}
JavaScript
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;
console.log(smallestSumSubarr(arr, 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 pos
5 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 =
10 min read
Smallest subarray with positive sum for all indices Given an array arr[] of size N. The task is to determine the minimum length of a subarray starting from index i, such that the sum of the subarray is strictly greater than 0. Calculate the length for all i's in the range 1 to N. If no such subarray exists, then return 0. Examples: Input: N = 3, arr[
8 min read
Smallest subarray with k distinct numbers We are given an array consisting of n integers and an integer k. We need to find the smallest subarray [l, r] (both l and r are inclusive) such that there are exactly k different numbers. If no such subarray exists, print -1 and If multiple subarrays meet the criteria, return the one with the smalle
14 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 a
6 min read