All problems
0303EasyArray

Paired Halves Interleave

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1470Shuffle the Array

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 flow logger keeps one batch in a single flat list readings of length 2 * half. The first half entries are the intake figures for pairs 0, 1, ..., half - 1 in that order, and the last half entries are the outflow figures for the same pairs in the same order, so readings[half + i] is the outflow that belongs with the intake readings[i].

Rebuild the batch pair by pair: the intake of pair 0, then the outflow of pair 0, then the intake of pair 1, then the outflow of pair 1, and so on to the last pair.

Return the rebuilt list, which holds the same 2 * half figures in that paired order.

Examples

Example 1

Input
readings = [3,7,2,9,4,1], half = 3
Output
[3,9,7,4,2,1]

The intake figures are 3, 7 and 2 and the outflow figures are 9, 4 and 1, so pair 0 reads 3 then 9, pair 1 reads 7 then 4, and pair 2 reads 2 then 1.

Example 2

Input
readings = [5,4,3,2,1,6], half = 3
Output
[5,2,4,1,3,6]

Intake 5 belongs with outflow 2, intake 4 with outflow 1, and intake 3 with outflow 6.

Constraints

  • 1 <= half <= 500
  • readings.length == 2 * half
  • 1 <= readings[i] <= 10^3

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 interleave_halves(readings: list[int], half: int) -> list[int]:
Java
public int[] interleaveHalves(int[] readings, int half)
September 7
Apply