FastPrepSplit Array Largest Sum

Split Array Largest Sum

Bloomberg LP logoBloomberg LP● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a nonnegative integer array nums and an integer d, split nums into exactly d nonempty contiguous segments.

For a split, calculate the sum of every segment and take the largest segment sum. Return the minimum possible value of that largest sum.

Function

splitArrayLargestSum(nums: int[], d: int) → long

Examples

Example 1

nums = [7,2,5,10,8]d = 2return = 18

Splitting as [7,2,5] and [10,8] gives segment sums 14 and 18. No two-segment split has a smaller maximum.

Example 2

nums = [1,2,3,4,5]d = 2return = 9

The optimal split is [1,2,3] and [4,5], whose largest sum is 9.

Example 3

nums = [1,4,4]d = 3return = 4

Exactly three nonempty segments force one element per segment.

Constraints

  • 1 <= nums.length <= 100000.
  • 0 <= nums[i] <= 1000000000.
  • 1 <= d <= nums.length.
  • The sum of nums fits in a signed 64-bit integer.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public long splitArrayLargestSum(int[] nums, int d) {
    // Write your code here.
}
nums[7,2,5,10,8]
d2
expected18
Checking account…