Cutting Ribbons - Problem

You are given an integer array ribbons, where ribbons[i] represents the length of the i-th ribbon, and an integer k. You may cut any of the ribbons into any number of segments of positive integer lengths, or perform no cuts at all.

For example, if you have a ribbon of length 4, you can:

  • Keep the ribbon of length 4
  • Cut it into one ribbon of length 3 and one ribbon of length 1
  • Cut it into two ribbons of length 2
  • Cut it into one ribbon of length 2 and two ribbons of length 1
  • Cut it into four ribbons of length 1

Your task is to determine the maximum length of ribbon, x, that allows you to cut at least k ribbons, each of length x. You can discard any leftover ribbon from the cuts.

If it is impossible to cut k ribbons of the same length, return 0.

Input & Output

Example 1 — Basic Case
$ Input: ribbons = [9,7,5], k = 3
Output: 5
💡 Note: We can cut the ribbons as follows: 9÷5=1 piece, 7÷5=1 piece, 5÷5=1 piece. Total = 3 pieces of length 5.
Example 2 — Multiple Cuts Per Ribbon
$ Input: ribbons = [7,5,9], k = 4
Output: 4
💡 Note: Cut ribbons: 7÷4=1 piece, 5÷4=1 piece, 9÷4=2 pieces. Total = 4 pieces of length 4.
Example 3 — Impossible Case
$ Input: ribbons = [5,7,9], k = 22
Output: 0
💡 Note: Even cutting all ribbons into pieces of length 1 gives us only 5+7+9=21 pieces, which is less than 22.

Constraints

  • 1 ≤ ribbons.length ≤ 105
  • 1 ≤ ribbons[i] ≤ 105
  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Cutting Ribbons Problem Overviewribbons = [9,7,5], k = 3Length: 9Length: 7Length: 5Goal: Cut at least k=3 pieces of same lengthCut: 5Cut: 5Cut: 5Total cuts: 1 + 1 + 1 = 3 pieces ≥ kAnswer: 5
Understanding the Visualization
1
Input
Array of ribbon lengths and target count k
2
Process
Find maximum length x where total cuts ≥ k
3
Output
Maximum valid ribbon length or 0 if impossible
Key Takeaway
🎯 Key Insight: Use binary search on the answer space since feasibility is monotonic
Asked in
Google 25 Amazon 20 Meta 15
23.4K Views
Medium Frequency
~25 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen