Given a positive integer N and K where and . The task is to check whether any permutation of digits of **N** equals any power of **K**. If possible return “**True**” otherwise return “**False**“.

**Examples:**

Input: N = 96889010407, K = 7 Output: True Explanation: 96889010407 = 7^{13}Input : N = 123456789, K = 4 Output : False

**Approach:** The **Naive approach** is to generate all **permutation** of digits of **N** and then check one by one if any of them is divisible of any power of **K**.

**Efficient Approach:** We know that total numbers of all power of **K** will not be more than **log _{K}(10^{18})**, for eg: if

**K = 2**then there will be atmost 64 numbers of power of

**K**. We generate all power of

**K**and store it in array.

Now we iterate all numbers from array and check where it contains all digits of N or not.

**Below is the implementation of above approach:**

## C++

`// CPP implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check if N and K are anagrams ` `bool` `isValid(` `long` `long` `int` `N, ` `long` `long` `int` `K) ` `{ ` ` ` `multiset<` `int` `> m1, m2; ` ` ` ` ` `while` `(N > 0) { ` ` ` `m1.insert(N % 10); ` ` ` `N /= 10; ` ` ` `} ` ` ` ` ` `while` `(K > 0) { ` ` ` `m2.insert(K % 10); ` ` ` `K /= 10; ` ` ` `} ` ` ` ` ` `if` `(m1 == m2) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Function to check if any permutation of N exist ` `// such that it is some power of K ` `string anyPermutation(` `long` `long` `int` `N, ` `long` `long` `int` `K) ` `{ ` ` ` `long` `long` `int` `powK[100], Limit = ` `pow` `(10, 18); ` ` ` ` ` `// generate all power of K under 10^18 ` ` ` `powK[0] = K; ` ` ` ` ` `int` `i = 1; ` ` ` `while` `(powK[i - 1] * K < Limit) { ` ` ` `powK[i] = powK[i - 1] * K; ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// check if any power of K is valid ` ` ` `for` `(` `int` `j = 0; j < i; j++) ` ` ` `if` `(isValid(N, powK[j])) { ` ` ` `return` `"True"` `; ` ` ` `} ` ` ` ` ` `return` `"False"` `; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `long` `long` `int` `N = 96889010407, K = 7; ` ` ` ` ` `// function call to print required answer ` ` ` `cout << anyPermutation(N, K); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is written by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of above approach ` ` ` `# function to check if N ` `# and K are anagrams ` `def` `isValid(N, K): ` ` ` ` ` `m1 ` `=` `[] ` ` ` `m2 ` `=` `[] ` ` ` ` ` `while` `(N > ` `0` `) : ` ` ` `m1.append(N ` `%` `10` `) ` ` ` `N ` `/` `/` `=` `10` ` ` ` ` `while` `(K > ` `0` `) : ` ` ` `m2.append(K ` `%` `10` `) ` ` ` `K ` `/` `/` `=` `10` ` ` ` ` `if` `(m1 ` `=` `=` `m2): ` ` ` `return` `True` ` ` `return` `False` ` ` `# Function to check if any permutation ` `# of N exist such that it is some power of K ` `def` `anyPermutation(N, K): ` ` ` ` ` `powK ` `=` `[` `0` `] ` `*` `100` ` ` `Limit ` `=` `pow` `(` `10` `, ` `18` `) ` ` ` ` ` `# generate all power of ` ` ` `# K under 10^18 ` ` ` `powK[` `0` `] ` `=` `K ` ` ` ` ` `i ` `=` `1` ` ` `while` `(powK[i ` `-` `1` `] ` `*` `K < Limit) : ` ` ` `powK[i] ` `=` `powK[i ` `-` `1` `] ` `*` `K ` ` ` `i ` `+` `=` `1` ` ` ` ` `# check if any power of K is valid ` ` ` `for` `j ` `in` `range` `(i): ` ` ` `if` `(isValid(N, powK[j])) : ` ` ` `return` `"True"` ` ` ` ` `return` `"False"` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `96889010407` ` ` `K ` `=` `7` ` ` ` ` `# function call to print ` ` ` `# required answer ` ` ` `print` `(anyPermutation(N, K)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

**Output:**

True

**Time Complexity:** O(log_{K}(10^{18})^{2})

## Recommended Posts:

- Check if given number is a power of d where d is a power of 2
- Check if any permutation of a number is divisible by 3 and is Palindromic
- Check if any permutation of a large number is divisible by 8
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check whether a given Number is Power-Isolated or not
- Given a HUGE number check if it's a power of two.
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number is power of k using base changing method
- Compute power of power k times % m
- Find power of power under mod of a prime
- Check if a number is a power of another number
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- K difference permutation
- Permutation Coefficient
- Permutation and Combination in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.