Find minimum subarray sum

Medium Arrays & Hashing Kadane's Algorithm Original on GeeksforGeeks

Find minimum subarray sum is a medium arrays & hashing problem solved with the kadane's algorithm pattern. The best approach, optimal (kadane for minimum), runs in O(n) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.

Problem

Given an integer array, find the smallest sum of any non-empty contiguous subarray.

This is the mirror image of the maximum subarray problem, and it is the key step in the circular version.

Examples

Example 1

Input
arr = [3, -4, 2, -3, -1, 7, -5]
Output
-6
Why
The subarray [-4, 2, -3, -1] sums to -6.

Example 2

Input
arr = [2, 6, 8, 1, 4]
Output
1
Why
All values are positive, so the smallest single element wins.

Constraints

  • 1 <= arr.length <= 10^5
  • -10^4 <= arr[i] <= 10^4

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute forceO(n²)O(1)
Optimal (Kadane for minimum)O(n)O(1)

1Brute force

TimeO(n²)
SpaceO(1)

Sum every subarray and keep the smallest.

  1. For each start i, extend j to the end with a running sum.
  2. Track the minimum.
class Solution {
    static int smallestSumSubarray(int[] a, int size) {
        int best = Integer.MAX_VALUE;
        for (int i = 0; i < size; i++) {
            int sum = 0;
            for (int j = i; j < size; j++) {
                sum += a[j];
                best = Math.min(best, sum);
            }
        }
        return best;
    }
}

2Optimal (Kadane for minimum)

TimeO(n)
SpaceO(1)

The smallest subarray ending at i either extends the one ending at i - 1 or starts fresh. Carry the running sum only while it is negative, because a positive carry can only make things bigger.

  1. cur = best = a[0].
  2. cur = min(a[i], cur + a[i]).
  3. best = min(best, cur).
class Solution {
    static int smallestSumSubarray(int[] a, int size) {
        int cur = a[0], best = a[0];
        for (int i = 1; i < size; i++) {
            cur = Math.min(a[i], cur + a[i]);
            best = Math.min(best, cur);
        }
        return best;
    }
}

Edge cases to test

  • All positive numbers
  • Single element

Hints

Hint 1

This is Kadane's algorithm with min in place of max.

FAQ

What is the best time complexity for Find minimum subarray sum?

Optimal (Kadane for minimum) runs in O(n) time and O(1) extra space.

Which pattern does Find minimum subarray sum use?

It is a arrays & hashing problem that uses the kadane's algorithm pattern. Other problems with the same pattern: Kadane's Algorithm - Simple, Kadane's Algo - Circular Array - No Extra Space, Gas Station.

Is there a brute force solution for Find minimum subarray sum?

Yes. Brute force takes O(n²) time and O(1) space. Sum every subarray and keep the smallest.

Which edge cases should I test for Find minimum subarray sum?

All positive numbers; Single element.