Show problems as
Classic problem #1470EasyArray

Shuffle the Array

Real-world version: Paired Halves Interleave
Tracked in this browser only
Read Shuffle the Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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

Python
def interleave_halves(readings: list[int], half: int) -> list[int]:
Java
public int[] interleaveHalves(int[] readings, int half)