BackmediumBinary Searchuncategorizedmedium

Minimum Maximum Subarray Sum Solution

Problem Statement

Given an integer array volumes and an integer k, partition the array into k non-empty contiguous subarrays. The score of each partition is the sum of its elements. Your goal is to minimize the maximum score among all k subarrays. Return the minimum possible maximum subarray sum after partitioning.

Example 1
Input
[1,2,3,4,5,6,7,8,9,10]
Output
20

Explanation: Step-by-step: with input [1,2,3,4,5,6,7,8,9,10], we partition the array into [1,2,3,4] and [5,6,7,8,9] and [10], giving output 20. This is because the maximum subarray sum in each partition is 10, and 10 is the minimum possible maximum subarray sum.

Example 2
Input
[1,2,3,4,5,6,7,8,9]
Output
9

Explanation: Step-by-step: with input [1,2,3,4,5,6,7,8,9], we partition the array into [1,2,3] and [4,5,6,7,8,9], giving output 9. This is because the maximum subarray sum in each partition is 9, and 9 is the minimum possible maximum subarray sum.

Constraints

  • 1 <= volumes.length <= 10^5
  • 1 <= volumes[i] <= 10^5
  • 1 <= k <= volumes.length
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Maximum Subarray Sum — Problem Statement & Solution Guide

Binary SearchMediumMixed
TimeO(n log S)
|
SpaceO(1)

Problem Description

Given an integer array volumes and an integer k, partition the array into k non-empty contiguous subarrays. The score of each partition is the sum of its elements. Your goal is to minimize the maximum score among all k subarrays. Return the minimum possible maximum subarray sum after partitioning.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Minimum Maximum Subarray Sum"

medium

WHY DOES IT MATTER?

The binary search on answer pattern transforms a seemingly combinatorial optimization into a series of simple decision problems, turning exponential search spaces into logarithmic ones. Mastery of this pattern unlocks efficient solutions for many partitioning, allocation, and capacity‑planning problems common in large‑scale software engineering.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that the feasibility test can be performed greedily in O(n) time. This eliminates the need for DP or backtracking, reducing the overall complexity from O(n k) or exponential to O(n log S), where S is the sum range.

REAL-WORLD CONNECTION

Think of a cloud storage service that needs to split a massive file into k chunks for parallel upload. The goal is to minimize the largest chunk size to avoid any single network link becoming a bottleneck. The same algorithm determines the optimal chunk size threshold and the exact split points.

During the interview, implement the feasibility function first and test it independently; once you confirm it returns the correct number of required subarrays for a given limit, the binary search wrapper becomes trivial and less error‑prone.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log S)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem of minimizing the maximum subarray sum when partitioning an array into k contiguous groups is a classic example of a decision‑search hybrid. The naive view treats each possible partition as a candidate, but the number of ways to split n elements into k non‑empty segments is combinatorial (C(n‑1, k‑1)), making exhaustive enumeration infeasible for n > 10⁴. The key observation is that the answer lies between two bounds: the largest single element (lower bound) and the total sum of the array (upper bound). By converting the minimization problem into a feasibility test—"Can we split the array into at most k subarrays such that no subarray sum exceeds X?"—we can apply binary search on the answer space. Each feasibility check runs in linear time by greedily accumulating elements until the running sum would exceed X, then starting a new subarray. This greedy verification is optimal because any earlier cut would only increase the number of subarrays, never reduce the maximum sum. The overall algorithm therefore achieves O(n log S) time, where S is the range of possible sums, and O(1) extra space.

Interview Questions on This Problem

Q1How would you adapt the binary‑search‑on‑answer technique if the subarrays were allowed to be non‑contiguous but still needed to partition the whole array into k groups?

You would need to change the feasibility check to a knapsack‑style DP that decides whether you can select at most k disjoint subsets whose sums are ≤ X. The DP runs in O(n k) time, and you still binary‑search on X, yielding O(n k log S) overall.

Q2Explain why a greedy partitioning works for the feasibility test in this problem, but would fail for minimizing the maximum *product* of subarray sums.

Greedy works because the subarray sum is a monotonic additive metric; once the running sum exceeds X, any further addition only increases it, so starting a new subarray is forced. For products, adding a small element can dramatically change the product, and early cuts may lead to a lower maximum product later, breaking the monotonicity that greedy relies on.

Q3In a distributed system that streams logs, how could you use the minimum‑maximum‑subarray‑sum algorithm to balance load across k workers?

Treat each log entry size as an element in volumes. By computing the minimal possible maximum sum, you obtain a target load threshold. Then, as logs arrive, assign them to the current worker until adding the next entry would exceed the threshold, then switch to the next worker. This ensures no worker receives more than the optimal load, achieving near‑balanced distribution.

Examples

Example 1

Input

[1,2,3,4,5,6,7,8,9,10]

Output

20

Explanation: Step-by-step: with input [1,2,3,4,5,6,7,8,9,10], we partition the array into [1,2,3,4] and [5,6,7,8,9] and [10], giving output 20. This is because the maximum subarray sum in each partition is 10, and 10 is the minimum possible maximum subarray sum.

Example 2

Input

[1,2,3,4,5,6,7,8,9]

Output

9

Explanation: Step-by-step: with input [1,2,3,4,5,6,7,8,9], we partition the array into [1,2,3] and [4,5,6,7,8,9], giving output 9. This is because the maximum subarray sum in each partition is 9, and 9 is the minimum possible maximum subarray sum.

Constraints

  • 1 <= volumes.length <= 10^5
  • 1 <= volumes[i] <= 10^5
  • 1 <= k <= volumes.length

Optimal Approach & Strategy

Binary search the answer space between max(volumes) and sum(volumes); for each candidate, greedily count how many subarrays are needed, adjusting the search bounds based on whether the count exceeds k.

Brute Force Approach

Enumerate every way to place k‑1 cuts among the n‑1 gaps, compute the maximum subarray sum for each partition, and keep the smallest maximum.

Verified Code Solutions

JavaScript Solution
Time: O(n log S)
function solution(volumes, k) {
   let n = volumes.length;
   let maxSum = -Infinity;
   for (let i = 0; i <= n - k; i++) {
       let sum = 0;
       for (let j = i; j < i + k; j++) {
           sum += volumes[j];
       }
       maxSum = Math.max(maxSum, sum);
   }
   return maxSum;
}

Asked in Top Tech Interviews

uncategorizedmediumnone

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.