Subarray Sums Divisible by K
Real-world version: Full Turn Command BlocksThe statement here is our own: the same inputs, outputs and technique.
A turntable is driven by a stepper motor that takes perTurn steps to bring the platter round once. You are given the queue of commands sent to the motor: steps[i] is a signed step count, positive to drive the platter forward and negative to drive it back.
A block is one or more commands that sit next to each other in the queue. A block is settling when running only that block leaves the platter pointing the way it pointed before the block began, which happens exactly when the step counts in the block add up to a multiple of perTurn. Negative multiples count, and so does a total of zero.
Return how many settling blocks the queue holds.
Examples
Example 1
- Input
- steps = [4, 5, 0, -2, -3, 1], perTurn = 5
- Output
- 7
Seven blocks total a multiple of 5: the whole queue at 5, `[5]`, `[5, 0]`, `[5, 0, -2, -3]` at 0, `[0]`, `[0, -2, -3]` at -5 and `[-2, -3]` at -5.
Example 2
- Input
- steps = [-1, 4], perTurn = 4
- Output
- 1
The block `[4]` totals 4, while `[-1]` totals -1 and `[-1, 4]` totals 3, neither of which is a multiple of 4.
Example 3
- Input
- steps = [2, -2, 2, -2], perTurn = 2
- Output
- 10
Every command is already a multiple of 2, so each of the ten blocks totals a multiple of 2 as well.
Constraints
- 1 <= steps.length <= 3 * 10^4
- -10^4 <= steps[i] <= 10^4
- 2 <= perTurn <= 10^4
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def count_settling_blocks(steps: list[int], perTurn: int) -> int:public int countSettlingBlocks(int[] steps, int perTurn)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Check if All the Integers in a Range Are CoveredReal-world version: Patrol Coverage AuditEasy
- Subarray Sum Equals KReal-world version: Trail Elevation StretchesMedium
- Contiguous ArrayReal-world version: Balanced Shift StretchMedium
- Count Number of Nice SubarraysReal-world version: Runs With K Odd ReadingsMedium