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));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem