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“.
Input: N = 96889010407, K = 7 Output: True Explanation: 96889010407 = 713 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 logK(1018), 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:
Time Complexity: O(logK(1018)2)
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.
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if given number is a power of d where d is a power of 2
- Array containing power of 2 whose XOR and Sum of elements equals X
- 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 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
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- 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
- Print any pair of integers with sum of GCD and LCM equals to N
- Smallest non-zero substring which has any permutation divisible by 2^K
- Maximize minimum distance between repetitions from any permutation of the given Array
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Count array elements that can be maximized by adding any permutation of first N natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.