Check if N can be converted to the form K power K by the given operation | C ++ implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < int > kPowKform ; int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . count ( n ) ) return 1 ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) ) { answer = 1 ; break ; } } x /= 10 ; } return answer ; } void canBeConverted ( int n ) { if ( func ( n ) ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int N = 90 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . insert ( val ) ; } canBeConverted ( N ) ; return 0 ; }