Divide Chocolate
Hard
Topics
Given an array nums of chunk sweetness values and an integer target (the number of cuts), you split the bar into target + 1 contiguous pieces and keep the piece with the smallest total sweetness. Return the maximum total sweetness of the piece you can guarantee for yourself.
Example 1
Input: nums = [1,2,3,4,5,6,7,8,9], target = 5 Output: 6
Example 2
Input: nums = [5,6,7,8,9,1,2,3,4], target = 8 Output: 1
Example 3
Input: nums = [1,2,2,1,2,2,1,2,2], target = 2 Output: 5
Constraints
- 0 <= target < nums.length <= 10^4
- 1 <= nums[i] <= 10^5
Editorial — intuition, approach, complexity and pitfalls
Intuition
We cut the bar into target + 1 contiguous chunks (target friends plus ourselves), everyone takes one, and we get the smallest. Maximise that smallest piece.
The brute force is to enumerate cut positions — choose target split points among n-1 gaps — which is combinatorial and hopeless. A DP over "best achievable minimum using the first i chunks split j ways" works and is O(n^2 k), still far too slow at n = 10^4.
The unlock is to stop searching for the arrangement and search for the answer. Guess a candidate minimum x and ask a much easier question: can every piece be at least x? That is answerable greedily in one pass — walk left to right accumulating sweetness, and the moment the running total reaches x, close the piece and start a new one. This maximises how many pieces of size at least x we can make, because cutting as early as possible always leaves the most material behind.
So define pieces(x) = the greatest number of chunks each totalling at least x. Then x is achievable exactly when pieces(x) >= target + 1.
And pieces is monotonic: raising x can only make pieces harder to fill, so the count never increases. That monotonicity is what licenses binary search — the predicate "is x achievable" is true for every value up to some threshold and false above it, so we binary search for the threshold.
💡 This is binary search on the answer: the search space is the range of possible answers rather than the array, and each probe runs a linear feasibility check. Recognise it when the problem says "maximise the minimum" or "minimise the maximum".
Approach
- Search range is
[min(nums), sum(nums)]. The lower bound is the smallest single chunk — no minimum can be worse than the smallest piece taken alone; the upper bound is one piece containing everything. - For each
mid, computepieces(mid)greedily. - If
pieces(mid) >= target + 1,midis feasible: record it and try larger (lo = mid + 1). Otherwise shrink (hi = mid - 1). - Return the last feasible value recorded.
Note target + 1, not target: you are one of the eaters. Getting this off by one is the single most common way to fail the problem.
Dry Run
sweetness = [1,2,3,4,5,6,7,8,9], target = 5, so 6 pieces are needed. Expected 6.
lo hi mid pieces(>=mid) need >= 6 action
1 45 23 1 no too greedy, hi = 22
1 22 11 3 no too greedy, hi = 10
1 10 5 6 YES feasible, ans = 5, lo = 6
6 10 8 4 no too greedy, hi = 7
6 7 6 6 YES feasible, ans = 6, lo = 7
7 7 7 5 no too greedy, hi = 6
lo > hi, answer 6At x = 6 the greedy cuts [1,2,3] [4,5] [6] [7] [8] [9] — six pieces, smallest exactly 6. At x = 7 only five pieces fit, so 6 is the threshold. The search converges in six probes over a range of 45 values.
Complexity
- Time — O(n log(sum)): each feasibility check is one linear pass, and the search halves a range bounded by the total sweetness. At the constraint limit, sum is up to 10^4 * 10^5 = 10^9, so about 30 iterations of a 10^4 pass — roughly 3*10^5 operations, against a combinatorial enumeration.
- Space — O(1): a handful of integers; the greedy check allocates nothing. Note the sum reaches 10^9, which still fits a signed 32-bit int but leaves little headroom — accumulating in 64-bit is the safer habit, and would be required if either constraint grew.
Common Pitfalls
- Comparing against
targetinstead oftarget + 1. You eat too, so the bar splits into one more piece than there are friends. - Starting
loat 0 or 1 rather thanmin(nums). Not wrong — the search still converges — but it wastes probes, and starting at 0 muddies the invariant that every candidate is a plausible piece size. - Returning
loorhiwithout tracking the last feasible value. With this loop shape (while lo <= hi) the pointers end crossed, and which one holds the answer depends on the branch taken last. Recordinganson success sidesteps the reasoning entirely. - Writing the greedy to cut when the running total exceeds
xrather than reaches it. The condition is>=; using>rejects a piece that is exactly right. - Trying to make the pieces equal, or to balance them. Only the minimum matters; a greedy that hoards material to even things out produces fewer valid pieces.
- Searching the array positions instead of the answer range. The cut positions are not monotonic in any useful way, which is why the search has to be over sweetness values.
Full Solution — all languages
class Solution:
def maximizeSweetness(self, nums, target):
def pieces(x):
c, cur = 0, 0
for v in nums:
cur += v
if cur >= x:
c += 1
cur = 0
return c
lo, hi, ans = min(nums), sum(nums), 0
while lo <= hi:
mid = (lo + hi) // 2
if pieces(mid) >= target + 1:
ans = mid
lo = mid + 1
else:
hi = mid - 1
return ansimport java.util.*;
class Solution {
public int maximizeSweetness(int[] nums, int target) {
int lo = nums[0], hi = 0;
for (int x : nums) {
lo = Math.min(lo, x);
hi += x;
}
int ans = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (pieces(nums, mid) >= target + 1) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
}
int pieces(int[] nums, int x) {
int c = 0, cur = 0;
for (int v : nums) {
cur += v;
if (cur >= x) {
c++;
cur = 0;
}
}
return c;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int maximizeSweetness(vector<int>& nums, int target) {
int lo = nums[0], hi = 0;
for (int x : nums) {
lo = min(lo, x);
hi += x;
}
int ans = 0;
auto pieces = [&](int x) {
int c = 0, cur = 0;
for (int v : nums) {
cur += v;
if (cur >= x) {
c++;
cur = 0;
}
}
return c;
};
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (pieces(mid) >= target + 1) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var maximizeSweetness = function(nums, target) {
let lo = nums[0], hi = 0;
for (const x of nums) {
lo = Math.min(lo, x);
hi += x;
}
let ans = 0;
const pieces = (x) => {
let c = 0, cur = 0;
for (const v of nums) {
cur += v;
if (cur >= x) {
c++;
cur = 0;
}
}
return c;
};
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (pieces(mid) >= target + 1) {
ans = mid;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
};