Check if any permutation of N equals any power of K | C # implementation of above approach . ; Function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; Generate all power of K under 10 ^ 18 ; Check if any power of K is valid ; Driver code ; Function call to print required answer
using System ; using System . Collections . Generic ; class GfG { static bool isValid ( long N , int K ) { HashSet < long > m1 = new HashSet < long > ( ) ; HashSet < int > m2 = new HashSet < int > ( ) ; while ( N > 0 ) { m1 . Add ( N % 10 ) ; N /= 10 ; } while ( K > 0 ) { m2 . Add ( K % 10 ) ; K /= 10 ; } if ( m1 . Equals ( m2 ) ) { return true ; } return false ; } static String anyPermutation ( long N , int K ) { int [ ] powK = new int [ 100 + 1 ] ; int Limit = ( int ) Math . Pow ( 10 , 18 ) ; powK [ 0 ] = K ; int i = 1 ; while ( powK [ i - 1 ] * K < Limit && i < 100 ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( int j = 0 ; j < i ; j ++ ) { if ( ! isValid ( N , powK [ j ] ) ) { return " True " ; } } return " False " ; } public static void Main ( String [ ] args ) { long N = 96889010407 ; int K = 7 ; Console . WriteLine ( anyPermutation ( N , K ) ) ; } }