Show problems as
Classic problem #1891MediumArrayBinary Search

Cutting Ribbons

Real-world version: The Longest Equal Lengths From the Reels
Tracked in this browser only
Read Cutting Ribbons on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Reels of tape have the lengths reels. A reel may be cut into any number of pieces of whole-number lengths and any offcut may be thrown away, but pieces can never be joined together.

Obtain at least k pieces, all of the same whole-number length. Return the greatest length they can have, or 0 when not even a length of one can be managed.

Examples

Example 1

Input
reels = [9, 7, 5], k = 3
Output
5

At a length of five each reel yields at least one piece, which is three in all. At six the reel of five yields nothing and the other two yield one apiece, leaving two pieces.

Example 2

Input
reels = [7, 5, 9], k = 4
Output
4

At a length of four the reels yield one, one and two pieces, four in all. At five they yield one, one and one, which is one short.

Example 3

Input
reels = [5, 7, 9], k = 22
Output
0

Even at a length of one the reels yield only twenty-one pieces between them, so no length can reach twenty-two.

Constraints

  • 1 <= reels.length <= 10^5
  • 1 <= reels[i] <= 10^5
  • 1 <= k <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def max_length(reels: list[int], k: int) -> int:
Java
public int maxLength(int[] reels, int k)