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

# Reordered Power of 2 Python Solution

> Tested Python solution for LeetCode 869 with 36 pytest cases. Generate a practice environment with lcpy.

LeetCode 869, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Sorting](/catalog/topics/sorting), [Counting](/catalog/topics/counting), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/reordered-power-of-2/description/).

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

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

## Problem

You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return `true` if and only if we can do this so that the resulting number is a power of two.

### Examples

```
Input: n = 1
Output: true
```

```
Input: n = 10
Output: false
```

### Constraints

* 1 \<= n \<= 10^9

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(d log d) where d is the number of digits in n
    # Space: O(d)
    def reordered_power_of_2(self, n: int) -> bool:
        digits = sorted(str(n))
        return any(sorted(str(1 << k)) == digits for k in range(31))
```

## Complexity

| Time | Space |
| - | - |
| O(d log d) where d is the number of digits in n | O(d) |

## Tags


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