Skip to main content

Split Array Largest Sum - Video Solutions

HardArrayBinary SearchDynamic ProgrammingGreedyPrefix Sum

BS 19. Painter's Partition and Split Array - Largest Sum

take U forward
11:20284,055 views
10 video solutions available

Split Array Largest Sum - Video Solution

Watch 10 video solutions for Split Array Largest Sum, a hard level problem involving Array, Binary Search, Dynamic Programming. This walkthrough by take U forward has 284,055 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.

Return the minimized largest sum of the split.

A subarray is a contiguous part of the array.

 

Example 1:

Input: nums = [7,2,5,10,8], k = 2
Output: 18
Explanation: There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.

Example 2:

Input: nums = [1,2,3,4,5], k = 2
Output: 9
Explanation: There are four ways to split nums into two subarrays.
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 106
  • 1 <= k <= min(50, nums.length)
Read full problem with examples

Approach Overview

Problem Overview: Given an integer array nums and an integer k, split the array into k non-empty contiguous subarrays. The goal is to minimize the largest sum among these subarrays. You must decide where to split so that the maximum subarray sum is as small as possible.

Approach 1: Dynamic Programming with Prefix Sum (O(n² * k) time, O(n * k) space)

This method builds the solution by considering every possible split position. Use a prefix sum array to compute subarray sums in O(1). Let dp[i][j] represent the minimum possible largest subarray sum when splitting the first i elements into j parts. For each state, iterate over all previous split points p and minimize max(dp[p][j-1], sum(p+1..i)). The prefix sum array avoids recomputing sums repeatedly. This approach guarantees the optimal answer but becomes expensive for large n because every state checks multiple partitions.

Approach 2: Binary Search with Greedy Split (O(n log(sum(nums))) time, O(1) space)

The key observation: the answer lies between max(nums) and sum(nums). Instead of testing every partition, perform binary search on this range. For a candidate maximum sum mid, scan the array and greedily form subarrays while the running sum stays ≤ mid. When the sum exceeds mid, start a new subarray. This greedy check runs in O(n) and tells you how many subarrays are required. If more than k subarrays are needed, the limit is too small; increase the search range. Otherwise, try a smaller maximum. This technique combines greedy validation with binary search to efficiently converge on the optimal value.

Recommended for interviews: The binary search + greedy solution is what most interviewers expect. It reduces the search space dramatically and runs in O(n log(sum)). The dynamic programming approach shows strong understanding of partition DP and is useful for reasoning about the problem, but the optimized binary search approach demonstrates algorithmic insight and scalability.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming with Prefix SumO(n² * k)O(n * k)Useful for understanding partition DP or when constraints are small
Binary Search with Greedy SplitO(n log(sum(nums)))O(1)Optimal solution for large arrays and the expected interview approach