File: shuffle-the-array/solution.py

Date: 2026-06-06

Time: 19:06

shuffle-the-array/solution.py

Purpose

This file solves LeetCode 1470 — Shuffle the Array. It takes an array of 2n elements structured as [x1, x2, ..., xn, y1, y2, ..., yn] and interleaves the two halves to produce [x1, y1, x2, y2, ..., xn, yn].

Key Components

Solution.shuffle(nums, n) — The sole method. Takes the full array and the half-length n, returns a new list with elements interleaved.

Patterns

Dependencies

Flow

1. Initialize empty result list.

2. Loop i from 0 to n - 1:

3. Return result.

For input [2, 5, 1, 3, 4, 7] with n = 3: the loop pairs (2,3), (5,4), (1,7) → output [2, 3, 5, 4, 1, 7].

Invariants

Error Handling

None. The method trusts its inputs — no bounds checking on n vs len(nums). This is standard for LeetCode solutions where input constraints are guaranteed by the problem.

Topics to Explore

Beliefs