← Back to challenges

Check If a Permutation of Digits Is a Power of Two

PythonHardalgorithmsconditionslogicnumbersvalidation

Instructions

The function is given an integer n. Find out if any permutation of digits of n such that the first digit is not 0 is equal to 2^k, return True / False.

Examples

check_power2(1) ➞ True
# 2^0 == 1

check_power2(13) ➞ False
# Neither 13 or 31 is a power of 2.

check_power2(61) ➞ True
# 2^4 == 16

check_power2(33) ➞ False
# 33 is not a power of 2.

check_power2(562) ➞ True
# 2^8 == 256

check_power2(921) ➞ False
# None of 921, 912, 291, 219, 192, 129 is a power of 2.

Notes

The input number 0 < n < pow(2, 64) within the unsigned long long int range in C++.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.