
Problem Statement
Given an integer n
, your task is to determine if it's possible to rearrange its digits to form a new number that is a power of two. In this rearrangement, the leading digit must not be zero. You need to decide and return true
if it's feasible to rearrange the digits to generate a number that's a power of two, otherwise return false
.
Examples
Example 1
Input:
n = 1
Output:
true
Example 2
Input:
n = 10
Output:
false
Constraints
1 <= n <= 109
Approach and Intuition
To tackle this problem, the central idea is to generate permutations of the digits of n
and check each permutation to see if it forms a power of two. Here are the steps involved in more detail:
- Extract Digits: Start by converting the integer
n
into its constituent digits. - Generate Permutations: Create all possible valid permutations of these digits. A valid permutation is one where the first digit is not zero.
- Check for Power of Two: For each valid permutation, convert it back into an integer and check if this integer is a power of two. An integer is a power of two if there exists an integer
x
such that (2^x) equals the integer. - Return the Result: If any one of the permutations meets the criterion of being a power of two, return
true
. If none of the permutations qualify, returnfalse
.
- The key challenge here lies in efficiently generating and checking permutations, especially given that
n
can be as large as10^9
, which implies up to 10 digits and potentially a factorial scale permutation count in the naive approach. - To optimize, we may also consider directly computing and storing powers of two within the permissible range and simply check if any permutation matches these precomputed values. This avoids the need to check if a number is a power of two from scratch each time.
These approaches hinge on the properties of digit rearrangement and the mathematical properties of powers of two. By exploring these efficiently, we can determine the answer to the given problem.
Solutions
- Java
class Solution {
public boolean isPowerOfTwoReordered(int number) {
int[] digitCount = computeDigitCount(number);
for (int power = 0; power < 31; power++)
if (Arrays.equals(digitCount, computeDigitCount(1 << power)))
return true;
return false;
}
public int[] computeDigitCount(int value) {
int[] countArray = new int[10];
while (value > 0) {
countArray[value % 10]++;
value /= 10;
}
return countArray;
}
}
The solution provided in Java addresses the problem of determining whether a number can be rearranged to form a power of two. The approach taken involves counting the digits of both the integer in question and the numbers that are powers of two, to see if both have identical digit counts. Here is a concise breakdown of how the solution works:
Function
isPowerOfTwoReordered
: This function determines if any permutation of the digits of a given number (number
) is a power of two. It does so by comparing the digit count of the number with the digit count of powers of two from1
(which is2^0
) to2^30
.- Compute the digit count of
number
usingcomputeDigitCount
. - Use a loop to iterate through powers of two from
2^0
to2^30
(as(1 << power)
, where<<
is the left shift operator). - For each power, compute its digit count and compare with the digit count of
number
. - If any digit count matches, return
true
indicating that a permutation ofnumber
is indeed a power of two. - If no matches are found by the end of the loop, return
false
.
- Compute the digit count of
Function
computeDigitCount
: This function takes an integervalue
and returns an array of ten integers each corresponding to the count of digits from 0 to 9 invalue
.- Initialize an array
countArray
with ten zeros. - Use a while loop to process each digit of
value
.- Increment the count of the corresponding digit in
countArray
. - Trim the last digit from
value
by integer division by 10.
- Increment the count of the corresponding digit in
- Once all digits are counted, return
countArray
.
- Initialize an array
Through this method, you can easily check if any permutation of an integer's digits can form a power of two by leveraging basic digit counting and array comparison techniques. This solution works efficiently within the constraints given the limited number of power of two checks (only up to 2^30
for a typical 32-bit integer).
No comments yet.