‹ back to the board

Wednesday, September 9

Adeptalgorithms & data structures

The Toll-Ledger of Emberford Bridge

# The Toll-Ledger of Emberford Bridge

The keeper of Emberford Bridge writes one number in her ledger each day: coins taken (a positive number), coins repaid to travellers (a negative number), or nothing at all (zero). The guild does not audit single days — it audits stretches, meaning any run of one or more consecutive days. A stretch is called *clean* when its net total is an exact multiple of m. Zero is an exact multiple of every m, and so is any negative multiple.

Write countStretches(days, m).

  • days is a list of integers, the ledger in order. It may be empty, and its entries may be negative, zero, or positive.
  • m is an integer, at least 1.

Return an integer: the number of contiguous, non-empty stretches of days whose sum is an exact multiple of m. Two stretches are different if they begin on different days or end on different days, even when their sums are equal. An empty ledger yields 0.

  • hash-map counting
  • Adept

examples

  1. case 1days of [3, 1, 2, -4, 5], m of 34 — the clean stretches are [3], [1, 2], [3, 1, 2] and [2, -4, 5]
  2. case 2days of [-2, 2, -5], m of 53 — [-2, 2] totals 0, [-5] totals -5, and [-2, 2, -5] totals -5; all three are exact multiples of 5
  3. case 3days of [-5, 6, 1], m of 31 — only [6] is clean; the totals -5, 1, 2, 7 and -4 of the other stretches are not multiples of 3
  4. case 4days of [], m of 40
reward
320 XP
signature
countStretches
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…
  • market week
  • debts count as clean
  • empty ledger
  • the negative leftover
  • a single unclean day
  • a single empty day
  • every stretch divides by one
  • four idle days
  • a fortnight and more of tolls
The Toll-Ledger of Emberford Bridge · bitwick