> ## 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.

# Sort Array By Parity II Python Solution

> Tested Python solution for LeetCode 922 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 922, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/sort-array-by-parity-ii/description/).

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

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

## Problem

\<p>Given an array of integers \<code>nums\</code>, half of the integers in \<code>nums\</code> are \<strong>odd\</strong>, and the other half are \<strong>even\</strong>.\</p>

\<p>Sort the array so that whenever \<code>nums\[i]\</code> is odd, \<code>i\</code> is \<strong>odd\</strong>, and whenever \<code>nums\[i]\</code> is even, \<code>i\</code> is \<strong>even\</strong>.\</p>

\<p>Return \<em>any answer array that satisfies this condition\</em>.\</p>

### Examples

```
Input: nums = [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
```

```
Input: nums = [2,3]
Output: [2,3]
```

### Constraints

* 2 \<= nums.length \<= 2 \* 10^4
* nums.length is even.
* Half of the integers in nums are even.
* 0 \<= nums\[i] \<= 1000

**Follow up:** Could you solve it in-place?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def sort_array_by_parity_ii(self, nums: list[int]) -> list[int]:
        even = 0
        odd = 1
        while even < len(nums) and odd < len(nums):
            if nums[even] % 2 == 0:
                even += 2
            elif nums[odd] % 2 == 1:
                odd += 2
            else:
                nums[even], nums[odd] = nums[odd], nums[even]
                even += 2
                odd += 2
        return nums
```

## Complexity

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

## Tags


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