Shuffle the Array
Real-world version: Paired Halves InterleaveThe 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.
def interleave_halves(readings: list[int], half: int) -> list[int]:public int[] interleaveHalves(int[] readings, int half)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 7 test cases: 7 passed
- Passed: 2 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's