All problems
1035MediumArrayBinary Search

The Longest Equal Lengths From the Reels

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1891Cutting Ribbons

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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