Trains the technique from
LeetCode 2364Count Number of Bad PairsThis 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 single row of a theatre is filled from left to right. Seat i, numbering the seats from 0, is held by a guest whose ticket number is tickets[i].
Take any two seats i and j with i < j. The pair is in step when the number of seats between them matches the rise in ticket number, that is when j - i == tickets[j] - tickets[i]. Every other pair is out of step.
Return how many pairs of seats are out of step. A pair is counted once, not once per order, and a seat is never paired with itself.
Example 1
The pair of seats 0 and 1 is in step: the seats are 1 apart and the ticket numbers 8 and 9 are also 1 apart. The pair 0 and 2 is out of step, 2 seats apart against a ticket rise of -5, and so is the pair 1 and 2, 1 seat apart against a rise of -6.
Example 2
The ticket number climbs by exactly one from each seat to the next, so for every one of the ten pairs the seat gap and the ticket rise are the same number.
Example 3
Seats 0 and 1 are in step, and so are seats 2 and 3, each pair being 1 seat apart with a ticket rise of 1. The other four pairs, namely 0 with 2, 0 with 3, 1 with 2 and 1 with 3, are all out of step.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_out_of_step(tickets: list[int]) -> int:public long countOutOfStep(int[] tickets)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.