Check if any permutation of a number without any leading zeros is a power of 2 or not
Last Updated :
21 Jun, 2022
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.
Examples:
Input: N = 46
Output: 64
Explanation:
The permutation of 46 which is perfect power of 2 is 64( = 26)
Input: N = 75
Output: No
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
const int TEN = 10;
void updateFreq( int n, int freq[])
{
while (n) {
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
bool areAnagrams( int a, int b)
{
int freqA[TEN] = { 0 };
int freqB[TEN] = { 0 };
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0; i < TEN; i++) {
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
bool isPowerOf2( int N)
{
for ( int i = 0; i < 32; i++) {
if (areAnagrams(1 << i, N)) {
cout << (1 << i);
return true ;
}
}
return false ;
}
int main()
{
int N = 46;
if (!isPowerOf2(N)) {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static int TEN = 10 ;
static void updateFreq( int n, int freq[])
{
while (n > 0 )
{
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
static boolean areAnagrams( int a, int b)
{
int freqA[] = new int [TEN];
int freqB[] = new int [TEN];
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0 ; i < TEN; i++)
{
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
static boolean isPowerOf2( int N)
{
for ( int i = 0 ; i < 32 ; i++)
{
if (areAnagrams( 1 << i, N))
{
System.out.print(( 1 << i));
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int N = 46 ;
if (!isPowerOf2(N))
{
System.out.print( "No" );
}
}
}
|
Python3
TEN = 10
def updateFreq(n, freq):
while (n):
digit = n % TEN
freq[digit] + = 1
n / / = TEN
def areAnagrams(a, b):
freqA = [ 0 ] * (TEN)
freqB = [ 0 ] * (TEN)
updateFreq(a, freqA)
updateFreq(b, freqB)
for i in range (TEN):
if (freqA[i] ! = freqB[i]):
return False
return True
def isPowerOf2(N):
for i in range ( 32 ):
if (areAnagrams( 1 << i, N)):
print ( 1 << i)
return True
return False
N = 46
if (isPowerOf2(N) = = 0 ):
print ( "No" )
|
C#
using System;
class GFG{
static int TEN = 10;
static void updateFreq( int n,
int []freq)
{
while (n > 0)
{
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
static bool areAnagrams( int a, int b)
{
int []freqA = new int [TEN];
int []freqB = new int [TEN];
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0; i < TEN; i++)
{
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
static bool isPowerOf2( int N)
{
for ( int i = 0; i < 32; i++)
{
if (areAnagrams(1 << i, N))
{
Console.Write((1 << i));
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
int N = 46;
if (!isPowerOf2(N))
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var TEN = 10;
function updateFreq(n, freq)
{
while (n) {
var digit = n % TEN;
freq[digit]++;
n = parseInt(n/TEN);
}
}
function areAnagrams(a, b)
{
var freqA = Array(TEN).fill(0);
var freqB = Array(TEN).fill(0);
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( var i = 0; i < TEN; i++) {
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
function isPowerOf2(N)
{
for ( var i = 0; i < 32; i++) {
if (areAnagrams(1 << i, N)) {
document.write( (1 << i));
return true ;
}
}
return false ;
}
var N = 46;
if (!isPowerOf2(N)) {
document.write( "No" );
}
</script>
|
Time Complexity: O((log10N)2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...