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

# Array of Doubled Pairs Python Solution

> Tested Python solution for LeetCode 954 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 954, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/array-of-doubled-pairs/description/).

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

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

## Problem

Given an integer array of even length `arr`, return `true` if it is possible to reorder `arr` such that `arr[2 * i + 1] = 2 * arr[2 * i]` for every `0 <= i < len(arr) / 2`, or `false` otherwise.

### Examples

```
Input: arr = [3,1,3,6]
Output: false
```

```
Input: arr = [2,1,2,6]
Output: false
```

```
Input: arr = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
```

### Constraints

* 2 \<= arr.length \<= 3 \* 10^4
* arr.length is even.
* -10^5 \<= arr\[i] \<= 10^5

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def can_reorder_doubled(self, arr: list[int]) -> bool:
        counts = Counter(arr)
        for value in sorted(counts, key=abs):
            need = counts[value]
            if need == 0:
                continue
            if need > counts[2 * value]:
                return False
            counts[value] = 0
            counts[2 * value] -= need
        return True
```

## Complexity

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

## Tags


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