📜  检查没有任何前导零的数字的任何排列是否为2的幂

📅  最后修改于: 2021-04-29 07:23:19             🧑  作者: Mango

给定一个整数N,任务是检查没有任何前导零的N的任何排列是否为2的幂。如果存在给定数字的任何这样的排列,则打印该排列。否则,打印No。

例子:

天真的方法:一个简单的解决方案是生成数字N的所有排列,对于每个排列,检查其是否为2的幂。如果发现是真的,则打印“是” 。否则,打印No。
时间复杂度: O( (log 10 N )!*(log 10 N)),其中N是给定的数字N。
辅助空间: O(1)

高效的方法:给定数字和给定数字的任何排列的位数始终是相同的。因此,要优化上述方法,只需检查给定数字的位数是否等于2的任意完美幂。如果确定为真,则打印2的幂。否则,打印No。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
const int TEN = 10;
 
// Function to update the frequency
// array  such that freq[i] stores
// the frequency of digit i to n
void updateFreq(int n, int freq[])
{
    // While there are digits
    // left to process
    while (n) {
 
        int digit = n % TEN;
 
        // Update the frequency of
        // the current digit
        freq[digit]++;
 
        // Remove the last digit
        n /= TEN;
    }
}
 
// Function that returns true if a
// and b are anagrams of each other
bool areAnagrams(int a, int b)
{
    // To store the frequencies of
    // the digits in a and b
    int freqA[TEN] = { 0 };
    int freqB[TEN] = { 0 };
 
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
 
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
 
    // Match the frequencies of
    // the common digits
    for (int i = 0; i < TEN; i++) {
 
        // If frequency differs for any
        // digit then the numbers are
        // not anagrams of each other
        if (freqA[i] != freqB[i])
            return false;
    }
 
    return true;
}
 
// Function to check if any permutation
// of a number is a power of 2 or not
bool isPowerOf2(int N)
{
    // Iterate over all possible perfect
    // power of 2
    for (int i = 0; i < 32; i++) {
 
        if (areAnagrams(1 << i, N)) {
 
            // Print that number
            cout << (1 << i);
            return true;
        }
    }
    return false;
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 46;
 
    // Function Call
    if (!isPowerOf2(N)) {
        cout << "No";
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static int TEN = 10;
 
// Function to update the frequency
// array such that freq[i] stores
// the frequency of digit i to n
static void updateFreq(int n, int freq[])
{
     
    // While there are digits
    // left to process
    while (n > 0)
    {
        int digit = n % TEN;
 
        // Update the frequency of
        // the current digit
        freq[digit]++;
 
        // Remove the last digit
        n /= TEN;
    }
}
 
// Function that returns true if a
// and b are anagrams of each other
static boolean areAnagrams(int a, int b)
{
     
    // To store the frequencies of
    // the digits in a and b
    int freqA[] = new int[TEN];
    int freqB[] = new int[TEN];
 
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
 
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
 
    // Match the frequencies of
    // the common digits
    for(int i = 0; i < TEN; i++)
    {
         
        // If frequency differs for any
        // digit then the numbers are
        // not anagrams of each other
        if (freqA[i] != freqB[i])
            return false;
    }
    return true;
}
 
// Function to check if any permutation
// of a number is a power of 2 or not
static boolean isPowerOf2(int N)
{
     
    // Iterate over all possible perfect
    // power of 2
    for(int i = 0; i < 32; i++)
    {
        if (areAnagrams(1 << i, N))
        {
             
            // Print that number
            System.out.print((1 << i));
            return true;
        }
    }
    return false;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number N
    int N = 46;
 
    // Function call
    if (!isPowerOf2(N))
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
TEN = 10
 
# Function to update the frequency
# array such that freq[i] stores
# the frequency of digit i to n
def updateFreq(n, freq):
     
    # While there are digits
    # left to process
    while (n):
        digit = n % TEN
 
        # Update the frequency of
        # the current digit
        freq[digit] += 1
 
        # Remove the last digit
        n //= TEN
     
# Function that returns true if a
# and b are anagrams of each other
def areAnagrams(a, b):
     
    # To store the frequencies of
    # the digits in a and b
    freqA = [0] * (TEN)
    freqB = [0] * (TEN)
 
    # Update the frequency of
    # the digits in a
    updateFreq(a, freqA)
 
    # Update the frequency of
    # the digits in b
    updateFreq(b, freqB)
 
    # Match the frequencies of
    # the common digits
    for i in range(TEN):
 
        # If frequency differs for any
        # digit then the numbers are
        # not anagrams of each other
        if (freqA[i] != freqB[i]):
            return False
 
    return True
 
# Function to check if any permutation
# of a number is a power of 2 or not
def isPowerOf2(N):
     
    # Iterate over all possible perfect
    # power of 2
    for i in range(32):
        if (areAnagrams(1 << i, N)):
 
            # Print that number
            print(1 << i)
            return True
         
    return False
 
# Driver Code
 
# Given number N
N = 46
 
# Function call
if (isPowerOf2(N) == 0):
    print("No")
 
# This code is contributed by code_hunt


C#
// C# program for
// the above approach
using System;
class GFG{
 
static int TEN = 10;
 
// Function to update the frequency
// array such that freq[i] stores
// the frequency of digit i to n
static void updateFreq(int n,
                       int []freq)
{
  // While there are digits
  // left to process
  while (n > 0)
  {
    int digit = n % TEN;
 
    // Update the frequency of
    // the current digit
    freq[digit]++;
 
    // Remove the last digit
    n /= TEN;
  }
}
 
// Function that returns true if a
// and b are anagrams of each other
static bool areAnagrams(int a, int b)
{
  // To store the frequencies of
  // the digits in a and b
  int []freqA = new int[TEN];
  int []freqB = new int[TEN];
 
  // Update the frequency of
  // the digits in a
  updateFreq(a, freqA);
 
  // Update the frequency of
  // the digits in b
  updateFreq(b, freqB);
 
  // Match the frequencies of
  // the common digits
  for(int i = 0; i < TEN; i++)
  {
    // If frequency differs for any
    // digit then the numbers are
    // not anagrams of each other
    if (freqA[i] != freqB[i])
      return false;
  }
  return true;
}
 
// Function to check if any
// permutation of a number
// is a power of 2 or not
static bool isPowerOf2(int N)
{
  // Iterate over all
  // possible perfect power of 2
  for(int i = 0; i < 32; i++)
  {
    if (areAnagrams(1 << i, N))
    {
      // Print that number
      Console.Write((1 << i));
      return true;
    }
  }
  return false;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given number N
  int N = 46;
 
  // Function call
  if (!isPowerOf2(N))
  {
    Console.Write("No");
  }
}
}
 
// This code is contributed by 29AjayKumar


输出:
64



时间复杂度: O((log 10 N) 2 )
辅助空间: O(log 10 N)