‹ back to the board

Thursday, September 3

Adeptalgorithms & data structures

The Bell-Ringer's Even Cut

# The Bell-Ringer's Even Cut

The bell-tower of bitwick has lost its ropes, and the ringers want k new ones — all cut to exactly the same whole-number length, so the peal comes out even. Up in the chandler's loft sits a pile of old hemp lines of assorted lengths.

A line may be cut into several pieces, but no piece may be spliced together from two lines, and every leftover scrap is thrown on the fire. So a line of length L yields at most floor(L / x) pieces of length x.

Arguments

  • ropes: a list of positive integers, the length of each line in the loft (in hands). It may be empty.
  • k: a positive integer, how many pieces the ringers need.

Return the greatest whole number x (with x >= 1) such that at least k pieces of length x can be cut from the pile. If no such length exists — that is, if even pieces of length 1 cannot meet the demand — return 0.

Write longestCut(ropes, k).

  • binary search
  • Adept

examples

  1. case 1ropes of 10, 8 and 5; k of 54 — at length 4 the lines give 2, 2 and 1 pieces, which is exactly 5; at length 5 they give only 4 pieces
  2. case 2ropes of 3 and 3; k of 100 — even pieces of length 1 yield only 6
  3. case 3a single rope of 7; k of 17 — the whole line is one piece
reward
310 XP
signature
longestCut
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • three lines, five pieces
  • demand cannot be met
  • single line, single piece
  • empty loft
  • exactly enough at length one
  • assorted short lines
  • one enormous coil
  • the whole loft
The Bell-Ringer's Even Cut · bitwick