int groups(int[] nums, int sum) {
int count = 1;
int s = 0;
for(int i : nums) {
s += i;
if(s > sum) {
count++;
s = i;
}
}
return count;
}
public int splitArray(int[] nums, int m) {
int x = 0;
int y = 0;
for(int i : nums) {
x = Math.max(x, i);
y += i;
}
while(x <= y) {
int mid = x+(y-x)/2;
int groups = groups(nums, mid);
if(groups <= m) {
y = mid-1;
}
else {
x = mid+1;
}
}
return x;
}