Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Binary search on the answer: the maximum subarray sum ranges from max(nums) to sum(nums). For each candidate, greedily partition the array — start a new split whenever the running sum exceeds the candidate. Check if k or fewer splits suffice.
If we can split with max sum = X, then X+1 also works (monotonic). Binary search on the maximum subarray sum from max(nums) to sum(nums), checking if k or fewer splits suffice.
Updated Feb 2026