Smallest sum contiguous subarray
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 ?> |
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)
Contact Us