900. Reordered Power of 2
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks if we can reorder the digits of a given integer n to form a number that is a power of two. A key insight is that two numbers are permutations of each other's digits if and only if they contain the exact same count of each digit (0 through 9). For example, 46 can be reordered to 64, and both numbers have one '4' and one '6'.
Solution
Reordered Power of 2 in Python
class Solution:
def reorderedPowerOf2(self, n: int) -> bool:
# Get the canonical representation of n's digits by sorting them.
# This creates a 'signature' for the multiset of digits.
n_sorted_str = sorted(str(n))
# Iterate through powers of 2.
# We only need to check up to 2^29 since n <= 10^9.
# Any reordering of n will have at most 10 digits (for n=10^9),
# and cannot form 2^30 or higher (smallest number with same digits as 2^30 is > 10^9).
for i in range(30):
# Calculate the current power of 2.
power_of_2 = 1 << i
# Get its canonical representation.
p_sorted_str = sorted(str(power_of_2))
# If the sorted lists of characters (digits) match, n can be reordered into a power of 2.
if n_sorted_str == p_sorted_str:
return True
# If no match is found after checking all relevant powers of 2.
return False
Reordered Power of 2 in Java
import java.util.Arrays;
class Solution {
public boolean reorderedPowerOf2(int n) {
// Get the canonical representation of n's digits by sorting them.
char[] n_chars = String.valueOf(n).toCharArray();
Arrays.sort(n_chars);
String n_sorted_str = new String(n_chars);
// Iterate through powers of 2.
// We only need to check up to 2^29 since n <= 10^9.
// Any reordering of n will have at most 10 digits (for n=10^9),
// and cannot form 2^30 or higher (smallest number with same digits as 2^30 is > 10^9).
for (int i = 0; i < 30; i++) {
// Calculate the current power of 2.
int powerOf2 = 1 << i;
// Get its canonical representation.
char[] p_chars = String.valueOf(powerOf2).toCharArray();
Arrays.sort(p_chars);
String p_sorted_str = new String(p_chars);
// If the sorted digit strings match, n can be reordered to a power of 2.
if (n_sorted_str.equals(p_sorted_str)) {
return true;
}
}
// If no match is found after checking all relevant powers of 2.
return false;
}
}
Reordered Power of 2 in C++
class Solution {
public:
bool reorderedPowerOf2(int n) {
// Convert n to its canonical string representation by sorting its digits.
string n_str = to_string(n);
sort(n_str.begin(), n_str.end());
// Iterate through powers of 2.
// We only need to check up to 2^29 since n <= 10^9.
// Any reordering of n will have at most 10 digits (for n=10^9),
// and cannot form 2^30 or higher (smallest number with same digits as 2^30 is > 10^9).
for (int i = 0; i < 30; ++i) {
// Calculate the current power of 2.
int powerOf2 = 1 << i;
// Get its canonical string representation.
string p_str = to_string(powerOf2);
sort(p_str.begin(), p_str.end());
// If the sorted digit strings match, n can be reordered to a power of 2.
if (n_str == p_str) {
return true;
}
}
// If no match is found after checking all relevant powers of 2.
return false;
}
};
Reordered Power of 2 in TypeScript
function reorderedPowerOf2(n: number): boolean {
// Helper function to get the canonical representation (sorted string of digits).
const getSignature = (num: number): string => {
return num.toString().split('').sort().join('');
};
// Get the signature of the input number n.
const nSignature = getSignature(n);
// Iterate through powers of 2.
// We only need to check up to 2^29 since n <= 10^9.
// Any reordering of n will have at most 10 digits (for n=10^9),
// and cannot form 2^30 or higher (smallest number with same digits as 2^30 is > 10^9).
for (let i = 0; i < 30; i++) {
// Calculate power of 2.
const powerOf2 = 1 << i;
// Get its signature.
const pSignature = getSignature(powerOf2);
// If signatures match, we found a valid reordering.
if (nSignature === pSignature) {
return true;
}
}
// If no match is found after checking all relevant powers of 2.
return false;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.