Given an integer N, the task is to check if any permutation of N without any leading zeros is a power of 2. If any such permutation of the given number exists, then print that permutation. Otherwise, print No.
Input: N = 46
The permutation of 46 which is perfect power of 2 is 64( = 26)
Input: N = 75
There is no possible permutation of 75 that results in perfect power of 2.
Naive Approach: A simple solution is to generate all permutations of the number N and for each permutation, check if it is a power of 2 or not. If found to be true, print Yes. Otherwise, print No.
Time Complexity: O((log10N)!*(log10N)), where N is the given number N.
Auxiliary Space: O(1)
Efficient Approach: The count of digits for the given number and for any permutation of the given number will always be the same. Therefore, to optimize the above approach, simply check if the count of digits of the given number is equal to that of any perfect power of 2 or not. If found to be true, print that power of 2. Otherwise, print No.
Below is the implementation of the above approach:
Time Complexity: O((log10N)2)
Auxiliary Space: O(log10N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- All possible numbers of N digits and base B without leading zeros
- Count of N-bit binary numbers without leading zeros
- Remove leading zeros from a Number given as a string
- Count numbers having N 0's and and M 1's with no leading zeros
- Check if any permutation of N equals any power of K
- Check if given number is a power of d where d is a power of 2
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- To check a number is palindrome or not without using any extra space
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Check if any permutation of a large number is divisible by 8
- Check if any permutation of a number is divisible by 3 and is Palindromic
- Check whether a given Number is Power-Isolated or not
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Find power of power under mod of a prime
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.