Split array - largest sum

Binary Search FAQs Hard

Given an integer array a of size n and an integer k. Split the array a into k non-empty subarrays such that the largest sum of any subarray is minimized. Return the minimized largest sum of the split.

Examples:

Input: a = [1, 2, 3, 4, 5], k = 3

Output:6

Explanation: There are many ways to split the array a[] into k consecutive subarrays. The best way to do this is to split the array a[] into [1, 2, 3], [4], and [5], where the largest sum among the three subarrays is only 6.

Input: a = [3,5,1], k = 3

Output: 5

Explanation: There is only one way to split the array a[] into 3 subarrays, i.e., [3], [5], and [1]. The largest sum among these subarrays is 5.

Input: a = [1, 2, 3, 4, 5], k = 2

Constraints

  •  1 ≤ n ≤ 104
  •  1 ≤ k ≤ n
  •  1 ≤ a[i] ≤ 104

Hints

  • Use binary search to find the smallest possible value of the maximum subarray sum.
  • "If mid is feasible (subarrays can be formed with a maximum sum ≤mid), search for a smaller maximum sum (high=mid). If mid is not feasible, search for a larger maximum sum (low=mid+1)."

Company Tags

Siemens Healthineers Splunk Airbnb Optum Ernst & Young Robinhood Reddit Seagate Technology Roche Rockstar Games Alibaba AMD Walmart MongoDB Rakuten Qualcomm GE Healthcare Etsy Deloitte Electronic Arts IBM Epic Games Broadcom NVIDIA Freshworks Google Microsoft Amazon Meta Apple Netflix Adobe