so i am solving this problem:https://leetcode.com/problems/split-array-largest-sum/description/
now below is my code:
class Solution {
private:
int func(vector& nums, int sum)
{
int cnt=1;
int cummSum=0;
for(int i=0;i<nums.size();i++)
{
if((cummSum+nums[i])<=sum)
{
cummSum=cummSum+nums[i];
}
else if((cummSum+nums[i])>sum)
{
cummSum=nums[i];
cnt++;
}
}
cout<<cnt;
return cnt;}
public:
int splitArray(vector& nums, int k) {
int cnt=0;
if(nums.size()<k)
{
return -1;
}
int minCap = *max_element(nums.begin(), nums.end());
cout<<"minCap "<<minCap;
int maxCap = accumulate(nums.begin(), nums.end(), 0);
for(int sum=minCap;sum<=maxCap;sum++)
{
cnt=func(nums,sum);
if(cnt==k)
{
return sum;
}
}
return 1;}
};
why is the below testcase failing:[2,3,1,1,1,1,1] k=5