All problems
0461MediumArrayBinary Search

Longest Lead Cut From The Reels

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2226Maximum Candies Allocated to K Children

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.

A depot holds cable on reels, reels[i] being the whole number of metres left on reel i. An order calls for leads service leads, and every lead must be cut to the same whole number of metres.

A reel can be cut into as many leads as fit, and whatever is left over on it is scrap. A lead is one continuous run of cable, so it must come from a single reel: two short offcuts cannot be joined into one lead. Making more leads than the order asks for is fine, and reels may be left untouched.

Return the greatest length, in whole metres, that lets the order be filled. Return 0 when the depot cannot fill it even with one-metre leads.

Examples

Example 1

Input
reels = [16, 9, 27], leads = 7
Output
6

At 6 metres the reels give 2, 1 and 4 leads, which is the 7 the order asks for. At 7 metres they give only 6.

Example 2

Input
reels = [4], leads = 5
Output
0

The single reel holds 4 metres, so even one-metre leads come to 4, one short of the order.

Example 3

Input
reels = [100, 100], leads = 4
Output
50

Each reel gives two 50-metre leads, filling the order of 4 exactly.

Constraints

  • 1 <= reels.length <= 10^5
  • 1 <= reels[i] <= 10^7
  • 1 <= leads <= 10^12

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 longest_lead(reels: list[int], leads: int) -> int:
Java
public int longestLead(int[] reels, long leads)
September 7
Apply