Trains the technique from
LeetCode 24Swap Nodes in 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.
An editing bench holds a reel: a run of film clips spliced end to end, each clip carrying its own frame count. Splices cannot be handed across, so the reel reaches you as a plain JSON array of frame counts in projection order, the clip nearest the head of the reel first. Hand your answer back in the same form. An array with nothing in it is a reel carrying no clips at all.
Work along the reel two clips at a time and swap each couple over: the first clip and the second clip trade places, then the third and the fourth trade places, and so on to the end. When the reel carries an odd number of clips, the last one has no partner and must be left sitting where it is.
Re-cut the splices, not the film: every clip keeps the frame count it arrived with, and only its position along the run may change. Make a single pass over the reel, in O(n) steps, holding only a constant amount of bookkeeping beyond the reel you return.
Example 1
The first couple trades to put 9 ahead of 4, and the second couple trades to put 7 ahead of 2.
Example 2
The leading couple trades over, and the clip holding 3 has nobody to pair with, so it keeps the tail position.
Example 3
A lone clip has no partner anywhere on the reel, so the reel is returned exactly as it came in.
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 swap_clips(reel: list[int]) -> list[int]:public int[] swapClips(int[] reel)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.