All problems
0997MediumArrayHash TableCounting

Pairing Meter Readings Into Whole Cycles

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1497Check If Array Pairs Are Divisible by k

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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