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 Largest Sum Contiguous Subarray is discussed. This is based on the fact that in order to find the minimum contiguous sum we can first make the elements of the original array negative ie. Replace each ar[i] by -ar[i] and then apply Kadane Algorithm. Clearly, if this is the max sum formed then the minimum sum would be the negative of this sum.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestSumSubarr( int arr[], int n)
{
for ( int i = 0; i < n; i++)
arr[i] = -arr[i];
int sum_here = arr[0], max_sum = arr[0];
for ( int i = 1; i < n; i++) {
sum_here = max(sum_here + arr[i], arr[i]);
max_sum = max(max_sum, sum_here);
}
return (-1) * max_sum;
}
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
import java.io.*;
class GFG
{
static int smallestSumSubarr( int arr[],
int n)
{
for ( int i = 0 ; i < n; i++)
arr[i] = -arr[i];
int sum_here = arr[ 0 ],
max_sum = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
sum_here = Math.max(sum_here +
arr[i], arr[i]);
max_sum = Math.max(max_sum,
sum_here);
}
return (- 1 ) * max_sum;
}
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));
}
}
|
Python3
def smallestSumSubarr(arr, n):
for i in range (n):
arr[i] = - arr[i]
sum_here = arr[ 0 ]
max_sum = arr[ 0 ]
for i in range ( 1 , n):
sum_here = max (sum_here + arr[i], arr[i])
max_sum = max (max_sum, sum_here)
return ( - 1 ) * max_sum
arr = [ 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 ]
n = len (arr)
print ( "Smallest sum:" ,
smallestSumSubarr(arr, n))
|
C#
using System;
class GFG
{
static int smallestSumSubarr( int []arr,
int n)
{
for ( int i = 0; i < n; i++)
arr[i] = -arr[i];
int sum_here = arr[0],
max_sum = arr[0];
for ( int i = 1; i < n; i++)
{
sum_here = Math.Max(sum_here +
arr[i], arr[i]);
max_sum = Math.Max(max_sum,
sum_here);
}
return (-1) * max_sum;
}
public static void Main ()
{
int []arr = {3, -4, 2, -3,
-1, 7, -5};
int n = arr.Length;
Console.WriteLine( "Smallest sum: " +
smallestSumSubarr(arr, n));
}
}
|
PHP
<?php
function smallestSumSubarr( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
$arr [ $i ] = - $arr [ $i ];
$sum_here = $arr [0];
$max_sum = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
$sum_here = max( $sum_here +
$arr [ $i ], $arr [ $i ]);
$max_sum = max( $max_sum , $sum_here );
}
return (-1) * $max_sum ;
}
$arr = array ( 3, -4, 2, -3, -1, 7, -5 );
$n = sizeof( $arr );
echo "Smallest sum: " ,
smallestSumSubarr( $arr , $n );
?>
|
Javascript
<script>
function smallestSumSubarr(arr, n)
{
for (let i = 0; i < n; i++)
arr[i] = -arr[i];
let sum_here = arr[0],
max_sum = arr[0];
for (let i = 1; i < n; i++)
{
sum_here = Math.max(sum_here +
arr[i], arr[i]);
max_sum = Math.max(max_sum,
sum_here);
}
return (-1) * max_sum;
}
let arr = [3, -4, 2, -3,
-1, 7, -5];
let n = arr.length;
document.write( "Smallest sum: " +
smallestSumSubarr(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...