Java ProgramsArraysFind Minimum Subarray Sum

Find Minimum Subarray Sum in Java

intermediate·  Arrays  ·  Array

Problem

The minimum subarray sum is the smallest possible sum of any contiguous run of elements within the array — including the possibility that a single very negative element is the best (lowest) choice on its own.

Given an array of integers, find the smallest sum achievable by any contiguous subarray.

Input
[3, -4, 2, -3, -1, 7, -5]
Output
Minimum subarray sum: -6

Java Program

Java
public class MinSubarraySum { public static void main(String[] args) { int[] arr = {3, -4, 2, -3, -1, 7, -5}; int minSum = arr[0]; for (int i = 0; i < arr.length; i++) { int sum = 0; // reset for each new starting index for (int j = i; j < arr.length; j++) { sum += arr[j]; // extend the subarray by one element if (sum < minSum) minSum = sum; } } System.out.println("Minimum subarray sum: " + minSum); } }

Output

Minimum subarray sum: -6

Core Logic

Computing the sum of every possible contiguous subarray directly, and keeping track of the smallest one seen, checks every candidate explicitly.

How It Works
  1. 1The outer loop picks a starting index i for a candidate subarray.
  2. 2The inner loop extends the subarray one element at a time, adding arr[j] to a running sum.
  3. 3After each extension, sum is compared against minSum, updating it whenever a smaller total is found.
  4. 4By the time both loops finish, every one of the array's contiguous subarrays has had its sum checked.
For [3, -4, 2, -3, -1, 7, -5], the subarray [-4, 2, -3, -1] sums to -6, the smallest total found across every subarray tried.
💡

Key Point: This checks every O(n²) subarray explicitly — the same exhaustive idea used to find a maximum subarray sum, just tracking the smaller total instead of the larger one.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: Every possible subarray's sum is computed directly by extending the inner loop's running total, so the nested loops do O(n²) work with no extra memory.

Key Concepts

nested for looprunning sumrunning minimum

Approach 2: Running Minimum (Optimized)

Java
public class MinSubarraySumOptimized { public static void main(String[] args) { int[] arr = {3, -4, 2, -3, -1, 7, -5}; int minEndingHere = arr[0]; int minSoFar = arr[0]; for (int i = 1; i < arr.length; i++) { minEndingHere = Math.min(arr[i], minEndingHere + arr[i]); // extend, or restart from here minSoFar = Math.min(minSoFar, minEndingHere); // smallest subarray ending anywhere so far } System.out.println("Minimum subarray sum: " + minSoFar); } }

Output

Minimum subarray sum: -6

Core Logic

At each position, deciding whether extending the previous running subarray still gives a lower sum than starting fresh right there finds the minimum in a single pass, without ever checking a subarray twice.

How It Works
  1. 1minEndingHere tracks the smallest sum of a subarray that ends exactly at the current position.
  2. 2minEndingHere = Math.min(arr[i], minEndingHere + arr[i]) decides, at each step, whether extending the previous subarray still beats starting over from arr[i] alone.
  3. 3minSoFar tracks the best (smallest) minEndingHere seen across the whole scan, updated after every step.
  4. 4By the end of one pass, minSoFar holds the answer — no nested loop was ever needed.
Scanning [3, -4, 2, -3, -1, 7, -5], minEndingHere resets to -4 once the leading 3 no longer helps, then keeps extending through 2, -3, -1 to reach -6, which becomes minSoFar.
💡

Key Point: This is the same running-decision idea behind Kadane's algorithm, mirrored for the smallest sum instead of the largest — extend the running subarray only while doing so keeps improving the answer.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Each element is visited exactly once, deciding whether to extend the running subarray or start fresh from the current element — no nested loop and no extra memory beyond two running variables.

Key Concepts

running minimumdynamic programming

Related Programs