> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Shuffle the Array Python Solution with Tests

> Tested Python solution for LeetCode 1470 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1470, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array). [View on LeetCode](https://leetcode.com/problems/shuffle-the-array/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1470   # by problem number
lcpy gen -s shuffle_the_array   # by problem name
```

## Problem

Given the array `nums` consisting of `2n` elements in the form `[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]`.

\<p>Return the array in the form \<code>\[x\<sub>1\</sub>,y\<sub>1\</sub>,x\<sub>2\</sub>,y\<sub>2\</sub>,...,x\<sub>n\</sub>,y\<sub>n\</sub>]\</code>.\</p>

### Examples

```
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
```

**Explanation:** Since x\<sub>1\</sub>=2, x\<sub>2\</sub>=5, x\<sub>3\</sub>=1, y\<sub>1\</sub>=3, y\<sub>2\</sub>=4, y\<sub>3\</sub>=7 then the answer is \[2,3,5,4,1,7].

```
Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
```

```
Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]
```

### Constraints

* 1 \<= n \<= 500
* nums.length == 2n
* 1 \<= nums\[i] \<= 10^3

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shuffle_the_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shuffle_the_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def shuffle(self, nums: list[int], n: int) -> list[int]:
        result: list[int] = [0] * (2 * n)
        for i in range(n):
            result[2 * i] = nums[i]
            result[2 * i + 1] = nums[i + n]
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.