Tutorialspoint
Problem
Solution
Submissions

Split Array Largest Sum

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C program to split an array into k non-empty subarrays such that the largest sum among these subarrays is minimized. Given an integer array nums and an integer k, find the minimized largest sum of the split.

Example 1
  • Input: nums = [7,2,5,10,8], k = 2
  • Output: 18
  • Explanation:
    • Split the array into [7,2,5] and [10,8].
    • Sum of first subarray: 7+2+5 = 14.
    • Sum of second subarray: 10+8 = 18.
    • The largest sum is max(14, 18) = 18.
    • This is the minimum possible largest sum.
Example 2
  • Input: nums = [1,2,3,4,5], k = 2
  • Output: 9
  • Explanation:
    • Split the array into [1,2,3,4] and [5].
    • Sum of first subarray: 1+2+3+4 = 10.
    • Sum of second subarray: 5.
    • The largest sum is max(10, 5) = 10.
    • Better split: [1,2,3] and [4,5] gives max(6, 9) = 9.
Constraints
  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 10^6
  • 1 ≤ k ≤ min(50, nums.length)
  • Time Complexity: O(n * log(sum) * k)
  • Space Complexity: O(1)
ArraysIBMSamsung
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

  • Use binary search on the answer - search between max element and sum of all elements
  • For each potential answer, check if it's possible to split array into k subarrays
  • Use a greedy approach to verify if a given sum limit allows k splits
  • The minimum possible answer is the maximum element in the array
  • The maximum possible answer is the sum of all elements
  • Binary search will find the minimum valid largest sum

Steps to solve by this approach:

 Step 1: Set binary search bounds - left as maximum element, right as sum of all elements
 Step 2: Use binary search to find the minimum possible largest sum
 Step 3: For each mid value, check if it's possible to split array into k subarrays
 Step 4: Implement canSplit function using greedy approach to count required subarrays
 Step 5: If current sum plus next element exceeds limit, start a new subarray
 Step 6: Count total subarrays needed and compare with k
 Step 7: Adjust binary search bounds based on whether split is possible or not

Submitted Code :