Divide Chocolate - Problem
You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the array sweetness.
You want to share the chocolate with your k friends so you start cutting the chocolate bar into k + 1 pieces using k cuts, each piece consists of some consecutive chunks.
Being generous, you will eat the piece with the minimum total sweetness and give the other pieces to your friends.
Find the maximum total sweetness of the piece you can get by cutting the chocolate bar optimally.
Input & Output
Example 1 — Basic Case
$
Input:
sweetness = [1,2,3,4,5,6,7,8,9], k = 2
›
Output:
14
💡 Note:
Optimal cuts create pieces [1,2,3,4,5] (sum=15), [6,7] (sum=13), [8,9] (sum=17). You get the minimum piece with sweetness 13. But better cuts [1,2,3,4] (sum=10), [5,6,7] (sum=18), [8,9] (sum=17) give minimum 10. The optimal is actually [1,2,3,4,5,6] (sum=21), [7] (sum=7), [8,9] (sum=17) → min=7. After trying all possibilities, the maximum minimum is 14.
Example 2 — Small Array
$
Input:
sweetness = [5,6,7,8,9,1,2,3,4], k = 8
›
Output:
1
💡 Note:
With k=8 cuts on 9 elements, we create 9 pieces of 1 element each. The minimum will be the smallest element, which is 1.
Example 3 — No Cuts Possible
$
Input:
sweetness = [1,2,2,1,2,2,1,2,2], k = 2
›
Output:
5
💡 Note:
Best cuts create pieces like [1,2,2] (sum=5), [1,2,2] (sum=5), [1,2,2] (sum=5). Each piece has sweetness 5, so minimum is 5.
Constraints
- 1 ≤ sweetness.length ≤ 104
- 1 ≤ sweetness[i] ≤ 105
- 0 ≤ k ≤ sweetness.length - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
Chocolate bar with sweetness array and k friends
2
Process
Make k cuts to create k+1 pieces, you get minimum
3
Output
Maximum possible sweetness of your piece
Key Takeaway
🎯 Key Insight: Use binary search on the answer space - search for the maximum achievable minimum sweetness rather than trying all cut combinations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code