Ctrl K

Equal Cable Pieces

Medium
A warehouse holds rolls of cable; rolls[i] is the length of the i-th roll in metres. The installers need need pieces of cable of the same length, and that length must be a positive whole number of metres. Any roll can be cut into several parts of whole lengths, or left uncut. Leftovers that are not used are thrown away; pieces cannot be joined together. Return the greatest length for which at least need pieces can be cut. If even pieces of length 1 are not enough, return 0.
Example 1
rolls=[12, 7, 4], need=4 → 4

Length 4 yields 3 + 1 + 1 = 5 pieces, which is enough. Length 5 yields only 2 + 1 + 0 = 3, too few.

Example 2
rolls=[3, 2], need=6 → 0

Even pieces of length 1 give only 5 in total, so the answer is 0.

Example 3
rolls=[10], need=3 → 3
Constraints
  • 1 ≤ len(rolls) ≤ 10⁵
  • 1 ≤ rolls[i] ≤ 10⁵
  • 1 ≤ need ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Shifted Sorted Array MediumFirst and Last Copy MediumFind in a Row-Chained Grid MediumFind in a Shifted Array With Repeats MediumMinimum of a Shifted List Medium
loading Python…
Input
rolls=[12, 7, 4], need=4
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.