Tutorialspoint
Problem
Solution
Submissions

Split Array Largest Sum

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to split an array into m non-empty continuous subarrays. The objective is to minimize the largest sum among these m subarrays. Your task is to find and return this minimized largest subarray sum.

Example 1
  • Input: nums = [7,2,5,10,8], m = 2
  • Output: 18
  • Explanation:
    • Step 1: We need to split the array into 2 subarrays.
    • Step 2: There are several ways to split the array, such as [7,2,5] and [10,8], or [7,2] and [5,10,8].
    • Step 3: Among all possible splits, the one with the minimum largest subarray sum is [7,2,5] and [10,8].
    • Step 4: The first subarray sum is 7+2+5=14, the second subarray sum is 10+8=18.
    • Step 5: The largest of these sums is 18, which is the minimum largest subarray sum possible.
Example 2
  • Input: nums = [1,2,3,4,5], m = 3
  • Output: 6
  • Explanation:
    • Step 1: We need to split the array into 3 subarrays.
    • Step 2: The optimal split is [1,2], [3], and [4,5].
    • Step 3: The subarray sums are 3, 3, and 9.
    • Step 4: The largest sum among these is 9.
    • Step 5: No other split results in a smaller maximum subarray sum.
Constraints
  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 10^6
  • 1 ≤ m ≤ min(50, nums.length)
  • Time Complexity: O(n * log(sum of array))
  • Space Complexity: O(1)
ArraysWiproCapgemini
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • This problem can be solved using binary search on the answer.
  • Define a search range where the minimum possible answer is max(nums) and the maximum possible answer is sum(nums).
  • For each mid value, check if it's possible to split the array into m or fewer subarrays such that the largest sum doesn't exceed mid.
  • If it's possible, search in the left half to find a smaller answer.
  • If it's not possible, search in the right half for a larger answer.

Steps to solve by this approach:

 Step 1: Define the search space for binary search, where left = maximum element in array and right = sum of all elements.

 Step 2: Perform binary search on this range to find the minimum largest subarray sum.
 Step 3: For each mid value, check if it's possible to split the array into m or fewer subarrays with maximum sum not exceeding mid.
 Step 4: If possible, search for an even smaller value by setting right = mid.
 Step 5: If not possible, search for a larger value by setting left = mid + 1.
 Step 6: The binary search continues until left and right converge, at which point we've found the minimum largest subarray sum.
 Step 7: Return the final left value as the answer.

Submitted Code :