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.
Example 1:
Input: n = 1 Output: true
Example 2:
Input: n = 10 Output: false
Constraints:
1 <= n <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The core idea is to check every single possible arrangement of the digits. We generate each possible arrangement, and then we check if that arrangement forms a power of 2.
Here's how the algorithm would work step-by-step:
def reordered_power_of_2(number):
from itertools import permutations
number_string = str(number)
# Generate all possible permutations of the digits.
for permutation_tuple in permutations(number_string):
rearranged_number_string = "".join(permutation_tuple)
# Skip leading zeros to avoid invalid numbers.
if rearranged_number_string[0] == '0' and len(rearranged_number_string) > 1:
continue
rearranged_number = int(rearranged_number_string)
# Check if the rearranged number is a power of 2.
if rearranged_number > 0 and (rearranged_number & (rearranged_number - 1)) == 0:
return True
# If no power of 2 is found after checking all permutations.
return False
The problem asks if we can rearrange a number to form a power of 2. The most efficient way to solve this is to avoid generating all rearrangements. Instead, we focus on the properties of power of 2s and use counting to determine if a rearrangement is possible.
Here's how the algorithm would work step-by-step:
def reordered_power_of_2(number):
number_string = str(number)
# Count the occurrences of each digit in the given number
number_digit_counts = {}
for digit in number_string:
number_digit_counts[digit] = number_digit_counts.get(digit, 0) + 1
# Iterate through powers of 2 within a reasonable range
for i in range(60):
power_of_2 = 1 << i
power_of_2_string = str(power_of_2)
# Only consider powers of 2 with the same number of digits
if len(power_of_2_string) != len(number_string):
continue
power_of_2_digit_counts = {}
for digit in power_of_2_string:
power_of_2_digit_counts[digit] = power_of_2_digit_counts.get(digit, 0) + 1
# Check if the digit counts match
if number_digit_counts == power_of_2_digit_counts:
return True
# If no power of 2 matches the digit counts, return False.
return False
Case | How to Handle |
---|---|
Input is 0 | Handle 0 as a special case as it doesn't fit the digit-permutation logic of powers of 2; could return true if power of 2 is 1 |
Input is a negative number | Return false immediately since powers of 2 are always positive. |
Input has leading zeros | The permutation logic should inherently handle leading zeros since it considers all digit arrangements. |
Input is a large number causing integer overflow in intermediate calculations | Use long data type in Java/C++ to avoid overflow during digit counts and comparisons with powers of 2. |
No power of 2 is a permutation of the input digits | Return false after checking all relevant powers of 2 (up to the length of the integer input). |
Input with repeated digits such as 11111... | The digit-counting approach should handle repeated digits correctly without special modification. |
Integer with digits sorted in descending order | Ensure the permutation algorithm correctly generates all possible combinations, including the largest magnitude number. |
The smallest possible valid input of '1' | Return true as 1 is a power of 2. |