Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Split Array Largest Sum problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Split Array Largest Sum problem solution, You are given an array nums which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.

Leetcode Split Array Largest Sum problem solution

Problem solution in Python.

class Solution:
    def splitArray(self, nums: List[int], m: int) -> int:
        dp = [[float("inf") for _ in range(len(nums))] for _ in range(m)]
        dp[0][0] = nums[0]
        for j in range(1, len(nums)):
            dp[0][j] = dp[0][j-1] + nums[j]
        
        for i in range(1,m):
            for j in range(i, len(nums)):
                dp[i][j] = min([max(dp[i-1][k],dp[0][j]-dp[0][k]) for k in range(i-1, j)])
        return dp[-1][-1]

Problem solution in Java.

class Solution {
    public int splitArray(int[] nums, int m) {
        int low = IntStream.of(nums).max().orElse(0);
        int high = IntStream.of(nums).sum();
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (split(nums, mid) > m) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
    
    private int split(int[] nums, int sum) {
        int ret = 1;
        int currentSum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (currentSum + nums[i] > sum) {
                ret++;
                currentSum = 0;
            }
            currentSum += nums[i];
        }
        return ret;
    }
}

Problem solution in C++.

bool can_split(vector<int> &nums,int m,long long int sum)
    {
        long long int cnt=1,cnt_sum=0;
        for (int i = 0; i <nums.size() ; ++i) {
            cnt_sum+=nums[i];
            if(cnt_sum>sum)
            {
                cnt_sum=nums[i];
                ++cnt;
                if (cnt>m) return false;
            }
        }
        return true;
    }
    int splitArray(vector<int>& nums, int m) 
    {
        long long left=*max_element(nums.begin(),nums.end());
        long long right=accumulate(nums.begin(),nums.end(),0ll);
        while(left<right)
        {
            long long mid=(left+right)>>1;
            if(can_split(nums,m,mid)) right=mid;
            else left=mid+1;
        }
        return left;
        
    }

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes