{x}
blog image

Reordered Power of 2

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

Solution Explanation for Reordered Power of 2

The problem asks whether a given integer n can be reordered to form a power of 2. The solution involves generating digit counts for n and comparing them to digit counts of powers of 2.

Approach:

  1. Digit Counting Function: A crucial function is created to count the occurrences of each digit (0-9) in a given number. This is done efficiently using arrays or vectors.

  2. Power of 2 Iteration: The code iterates through powers of 2 (starting from 1 and doubling each time). The loop continues until the power of 2 exceeds the maximum possible value that can be formed by rearranging the digits of n (which is 109 in this case, given the constraint).

  3. Comparison: In each iteration, the digit counts of the current power of 2 are compared to the digit counts of n. If they match, it means that the digits of the power of 2 are a permutation of the digits of n, indicating that the answer is true.

  4. Return Value: If no match is found after iterating through all relevant powers of 2, the function returns false.

Time Complexity Analysis:

  • The digit counting function convert() takes O(log10n) time, as the number of digits in n is proportional to the base-10 logarithm of n.
  • The outer loop iterates approximately log2(109) times, which simplifies to approximately 30 iterations.
  • The comparison of digit counts within the loop takes O(10) time (constant time, as we are dealing with a fixed number of digits).

Therefore, the overall time complexity is dominated by the loop, resulting in O(log n). The space complexity is O(1) because the space used by the digit count arrays is constant (10 elements).

Code Explanations (Python3 as Example):

class Solution:
    def reorderedPowerOf2(self, n: int) -> bool:
        def convert(n):
            cnt = [0] * 10 # Initialize array for digit counts. Size 10 for digits 0-9.
            while n: #Iterate while n is greater than 0
                n, v = divmod(n, 10) # Efficient way to get last digit and update n.
                cnt[v] += 1  # Increment count for the digit
            return cnt
 
        i, s = 1, convert(n) # s stores the digit counts for n. i is the power of 2 iterator.
        while i <= 10**9: # Iterate powers of 2 until max possible reordered number.
            if convert(i) == s: # Compare digit counts
                return True
            i <<= 1 # Efficient way to double i (equivalent to i*=2)
        return False
 

The other languages (Java, C++, Go) follow a similar structure, using the appropriate data structures and syntax for their respective languages. The core logic and complexity remain the same.