Show problems as
Classic problem #1497MediumArrayHash TableCounting

Check If Array Pairs Are Divisible by k

Real-world version: Pairing Meter Readings Into Whole Cycles
Tracked in this browser only
Read Check If Array Pairs Are Divisible by k on LeetCode(opens in a new tab)

The 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.

Python
def can_arrange(readings: list[int], k: int) -> bool:
Java
public boolean canArrange(int[] readings, int k)