The Standing Stones of Merrowdun Ley
The Surveyors' Lodge of Bitwick has walked the Merrowdun Ley, a single straight line of standing stones running out across the moor. At each stone they read its arcane charge, a whole number. Charged stones read positive. Drained stones read negative. Dead stones read zero.
The Lodge's binding rite can be anchored on any stretch of the ley. A stretch is one or more stones standing next to each other in the line. The rite holds only on a stretch whose charges add up to exactly the required amount. Before committing a circle of wardens, the Lodge wants to know how many stretches would serve.
Write countStretches(charges, target).
chargesis a list of integers, the readings of the stones in order along the ley. It may be empty. Values may be negative, zero or positive.targetis an integer, the required total. It may be negative, zero or positive.
Return an integer: the number of pairs of positions (i, j) with i <= j such that the sum charges[i] + charges[i+1] + ... + charges[j] equals target. Two stretches count separately if they begin or end at different stones, even when their sums match. An empty list has no stretches.
The ley may hold up to 100,000 stones.
examples
- case 1
charges of [1, 1, 1], target of 22 (the first two stones, and the last two stones) - case 2
charges of [0, 0, 0], target of 06 (every one of the six stretches of dead stones sums to 0) - case 3
charges of [3, -1, -2, 4, -4, 2], target of 04 ([3, -1, -2], [4, -4], [3, -1, -2, 4, -4] and [-2, 4, -4, 2]) - case 4
charges of [], target of 50
- reward
- 320 XP
- signature
countStretches
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- two overlapping pairs
- empty ley
- all dead stones with target zero
- mixed charges cancelling to zero
- single stone matching
- single stone not matching
- ascending charges
- negative target
- whole ley is the only stretch
- alternating charges