You are given an array of n non-negative integers and an integer k. Partition the array into exactly k non-empty contiguous groups (every element belongs to exactly one group, groups keep the original order). Among all such partitions, choose the one that minimizes the largest group sum. Return that minimized largest sum.
The answer is a single well-defined value. The intended approach binary-searches on the candidate largest sum: for a candidate limit L, a greedy left-to-right sweep counts the minimum number of groups whose sums each stay within L; feasibility is monotonic in L.
Line 1: an integer n, the length of the array.
Line 2: n space-separated non-negative integers.
Line 3: an integer k, the number of contiguous groups (1 ≤ k ≤ n).
A single integer: the minimum possible value of the largest group sum.
Example 1
Input
5 7 2 5 10 8 2
Expected
18
Explanation
Splitting as [7, 2, 5] and [10, 8] gives group sums 14 and 18; every other 2-way split has a larger maximum, so the answer is 18.
Example 2
Input
4 1 2 3 4 3
Expected
4
Explanation
The best 3-way split is [1, 2], [3], [4] with sums 3, 3, 4; the largest is 4, which cannot be reduced.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →