Reordered Power of 2

You are given an integer n. You may reorder its digits in any order, including keeping the original order, as long as the leading digit is not zero. Return true if you can reorder the digits so that the resulting number is a power of two.

30:00

Reordered Power of 2
medium
Free written solution
Topics
Companies

You are given an integer n. You may reorder its digits in any order, including keeping the original order, as long as the leading digit is not zero. Return true if you can reorder the digits so that the resulting number is a power of two.

Example 1:
Input: {"n":1}
Output: true
Constraints:
  • 1≤n≤1091 \leq n \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":1}

Reordering the digits of 1 cannot change which digits it has, only their order. So ask: does some power of 2 use exactly the same digits?

n

1

digit counts, digit 0 to 9

0

1

2

3

4

5

6

7

8

9

n

2^k

all 31 candidates 2^0 … 2^30

1

2

4

8

16

32

64

128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

524288

1048576

2097152

4194304

8388608

16777216

33554432

67108864

134217728

268435456

536870912

1073741824

len = wrong number of digits · ≠ = same length, different digits · ✓ = same digits

Variables
VariableValue
n1
DepthFunction Call
Stack empty
0/3