Check If Array Pairs Are Divisible by k
Real-world version: Pairing Meter Readings Into Whole CyclesThe statement here is our own: the same inputs, outputs and technique.
A meter log holds an even count of readings in readings, and one full cycle of the meter spans k units.
Return whether the readings can be split into pairs, every reading used in exactly one pair, so that each pair's two readings add up to a whole number of cycles.
Examples
Example 1
- Input
- readings = [2, 3, 5, 7, 8, 15], k = 5
- Output
- true
Leaving 5 with 15 gives a pair adding to two whole cycles. That leaves 2 with 8 and 3 with 7, each adding to one cycle.
Example 2
- Input
- readings = [1, 1], k = 5
- Output
- false
Both readings leave one over, and one plus one is not a whole cycle, so the only possible pair fails.
Example 3
- Input
- readings = [-1, 1], k = 5
- Output
- true
The negative reading leaves four over against the cycle, which completes the cycle with the one left over by the other reading.
Constraints
- readings.length is even.
- 2 <= readings.length <= 10^5
- -10^9 <= readings[i] <= 10^9
- 1 <= k <= 10^5
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 can_arrange(readings: list[int], k: int) -> bool:public boolean canArrange(int[] readings, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 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