Given an integer array arr[] of size n, find the minimum possible sum of any subarray.
Examples:
Input: arr[] = [3, -4, 2, -3, -1, 7, -5]
Output: -6
Explanation: Subarray with minimum sum is [-4, 2, -3, -1]= -6Input: arr[]= [2, 6, 8, 1, 4]
Output: 1
Explanation: Subarray with minimum sum is [1] = 1
Table of Content
[Naive Approach] - Try All Subarrays - O(n2) Time and O(1) Space
Consider all contiguous subarrays of the array arr[] using two nested loops.
Find the minimum possible sum among all.
#include <iostream>
#include<vector>
#include<climits>
using namespace std;
int minSubarraySum(vector<int>& arr)
{
int n = arr.size();
int ans = INT_MAX;
int temp = 0;
for (int i = 0; i < n; i++)
{
temp = 0;
// Check every possible subarray
// starting from i
for (int j = i; j < n; j++)
{
temp += arr[j];
ans = min(ans, temp);
}
}
return ans;
}
int main()
{
vector<int> arr = {3, -4, 2, -3, -1, 7, -5};
cout << minSubarraySum(arr);
return 0;
}
class GFG {
static int minSubarraySum(int[] arr) {
int n = arr.length;
int ans = Integer.MAX_VALUE;
int temp = 0;
for (int i = 0; i < n; i++) {
temp = 0;
// Check every possible subarray starting from i
for (int j = i; j < n; j++) {
temp += arr[j];
ans = Math.min(ans, temp);
}
}
return ans;
}
public static void main(String[] args) {
int[] arr = {3, -4, 2, -3, -1, 7, -5};
System.out.println(minSubarraySum(arr));
}
}
def minSubarraySum(arr):
n = len(arr)
ans = float('inf')
for i in range(n):
temp = 0
# Check every possible subarray
# starting from i
for j in range(i, n):
temp += arr[j]
ans = min(ans, temp)
return ans
if __name__ == "__main__":
arr = [3, -4, 2, -3, -1, 7, -5]
print(minSubarraySum(arr))
using System;
class GFG
{
static int minSubarraySum(int[] arr)
{
int n = arr.Length;
int ans = int.MaxValue;
for (int i = 0; i < n; i++)
{
int temp = 0;
// Check every possible subarray starting from i
for (int j = i; j < n; j++)
{
temp += arr[j];
ans = Math.Min(ans, temp);
}
}
return ans;
}
static void Main()
{
int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
Console.WriteLine(minSubarraySum(arr));
}
}
function minSubarraySum(arr) {
let n = arr.length;
let ans = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
let temp = 0;
// Check every possible subarray starting from i
for (let j = i; j < n; j++) {
temp += arr[j];
ans = Math.min(ans, temp);
}
}
return ans;
}
//Driver Code
let arr = [3, -4, 2, -3, -1, 7, -5];
console.log(minSubarraySum(arr));
Output
-6
[Expected Approach] - Using Kadane's Algorithm - O(n) Time and O(1) Space
It is a variation to the problem of finding the Maximum Subarray Sum 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 among all subarrays ending at that element. The result will be the minimum of all these values.
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 curMin, we can use the curMin 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 element 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 element is positive, it is always better to start a new subarray from the current element.
#include <iostream>
#include<climits>
#include<vector>
using namespace std;
int minSubarraySum(vector<int>&arr)
{
int n=arr.size();
// to store the minimum value that is ending
// up to the current index
int curMin = INT_MAX;
// to store the minimum answer
int ans = INT_MAX;
for (int i = 0; i < n; i++)
{
// if curMin > 0, then it could not possibly
// contribute to the minimum sum further
if (curMin > 0)
curMin = arr[i];
// else add the value arr[i] to curMin
else
curMin += arr[i];
// update ans
ans = min(ans, curMin);
}
//return the answer
return ans;
}
int main()
{
vector<int>arr = {3, -4, 2, -3, -1, 7, -5};
cout << minSubarraySum(arr);
return 0;
}
class GFG {
static int minSubarraySum(int[] arr) {
// to store the minimum value that is ending
// up to the current index
int curMin = Integer.MAX_VALUE;
// to store the minimum answer
int ans = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
// if curMin > 0, then it could not possibly
// contribute to the minimum sum further
if (curMin > 0)
curMin = arr[i];
// else add the value arr[i] to curMin
else
curMin += arr[i];
// update ans
ans = Math.min(ans, curMin);
}
// return the answer
return ans;
}
public static void main(String[] args) {
int[] arr = {3, -4, 2, -3, -1, 7, -5};
System.out.println(minSubarraySum(arr));
}
}
def minSubarraySum(arr):
# to store the minimum value that is ending
# up to the current index
curMin = float('inf')
# to store the minimum answer
ans = float('inf')
for num in arr:
# if curMin > 0, then it could not possibly
# contribute to the minimum sum further
if curMin > 0:
curMin = num
# else add the current number to curMin
else:
curMin += num
# update ans
ans = min(ans, curMin)
# return the answer
return ans
if __name__ == "__main__":
arr = [3, -4, 2, -3, -1, 7, -5]
print(minSubarraySum(arr))
using System;
class GFG
{
static int minSubarraySum(int[] arr)
{
// to store the minimum value that is ending
// up to the current index
int curMin = int.MaxValue;
// to store the minimum answer
int ans = int.MaxValue;
for (int i = 0; i < arr.Length; i++)
{
// if curMin > 0, then it could not possibly
// contribute to the minimum sum further
if (curMin > 0)
curMin = arr[i];
// else add the value arr[i] to curMin
else
curMin += arr[i];
// update ans
ans = Math.Min(ans, curMin);
}
// return the answer
return ans;
}
static void Main()
{
int[] arr = { 3, -4, 2, -3, -1, 7, -5 };
Console.WriteLine(minSubarraySum(arr));
}
}
function minSubarraySum(arr) {
// to store the minimum value that is ending
// up to the current index
let curMin = Number.MAX_SAFE_INTEGER;
// to store the minimum answer
let ans = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < arr.length; i++) {
// if curMin > 0, then it could not possibly
// contribute to the minimum sum further
if (curMin > 0)
curMin = arr[i];
// else add the value arr[i] to curMin
else
curMin += arr[i];
// update ans
ans = Math.min(ans, curMin);
}
// return the answer
return ans;
}
//Driver Code
let arr = [3, -4, 2, -3, -1, 7, -5];
console.log(minSubarraySum(arr));
Output
-6