Check if N can be converted to the form K power K by the given operation
Last Updated :
12 Apr, 2023
Given a positive number N, we have to find whether N can be converted to the form KK where K is also a positive integer, using the following operation any number of times :
- Choose any digit less than the current value of N, say d.
- N = N – d2, change N each time
If it is possible to express the number in the required form then print “Yes” otherwise print “No”.
Examples:
Input: N = 13
Output: Yes
Explanation:
For integer 13 choose d = 3 : N = 13 – 32 = 4, 4 is of the form 22. Hence, the output is 4.
Input: N = 90
Output: No
Explanation:
It is not possible to express the number 90 in required form.
Naive Approach:
To solve the problem mentioned above we will use Recursion. In each recursive step, traverse through all the digits of the current value of N, and choose it as d. This way all the search spaces will be explored and if in any of them N comes out to be of the form KK stop the recursion and return true. To check whether the number is of the given form, pre-store all such numbers in a set. This method takes O(DN), where D is the number of digits in N time and can be further optimized.
Below is the implementation of the given approach:
C++14
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static HashSet<Integer> kPowKform =
new HashSet<Integer>();
static int func( int n)
{
if (n <= 0 )
return 0 ;
if (kPowKform.contains(n))
return 1 ;
int answer = 0 ;
int x = n;
while (x > 0 )
{
int d = x % 10 ;
if (d != 0 )
{
if (func(n - d * d) == 1 )
{
answer = 1 ;
break ;
}
}
x /= 10 ;
}
return answer;
}
static void canBeConverted( int n)
{
if (func(n) == 1 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int N = 90 ;
for ( int i = 1 ; i <= 8 ; i++)
{
int val = 1 ;
for ( int j = 1 ; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
}
}
|
Python3
kPowKform = dict ()
def func(n):
global kPowKform
if (n < = 0 ):
return 0
if (n in kPowKform):
return 1
answer = 0
x = n
while (x > 0 ):
d = x % 10
if (d ! = 0 ):
if (func(n - d * d)):
answer = 1
break
x / / = 10
return answer
def canBeConverted(n):
if (func(n)):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 90
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 ,i + 1 ):
val * = i
kPowKform[val] = 1
canBeConverted(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static SortedSet< int > kPowKform = new SortedSet< int >();
static int func( int n)
{
if (n <= 0)
return 0;
if (kPowKform.Contains(n))
return 1;
int answer = 0;
int x = n;
while (x > 0)
{
int d = x % 10;
if (d != 0)
{
if (func(n - d * d) == 1)
{
answer = 1;
break ;
}
}
x /= 10;
}
return answer;
}
static void canBeConverted( int n)
{
if (func(n) == 1)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main()
{
int N = 90;
for ( int i = 1; i <= 8; i++)
{
int val = 1;
for ( int j = 1; j <= i; j++)
val *= i;
kPowKform.Add(val);
}
canBeConverted(N);
}
}
|
Javascript
<script>
var kPowKform = new Set();
function func(n)
{
if (n <= 0)
return 0;
if (kPowKform.has(n))
return 1;
var answer = 0;
var x = n;
while (x > 0)
{
var d = x % 10;
if (d != 0)
{
if (func(n - d * d)) {
answer = 1;
break ;
}
}
x = parseInt(x/10);
}
return answer;
}
function canBeConverted(n)
{
if (func(n))
document.write( "Yes" );
else
document.write( "No" );
}
var N = 90;
for ( var i = 1; i <= 8; i++) {
var val = 1;
for ( var j = 1; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
</script>
|
Efficient Approach:
In the recursive approach, we are solving the same subproblem multiple times i.e there are Overlapping Subproblems. So we can use Dynamic Programming and memorize the recursive approach using a cache or memorization table.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unordered_set< int > kPowKform;
int dp[100005];
int func( int n)
{
if (n <= 0)
return 0;
if (kPowKform.count(n))
return 1;
if (dp[n] != -1)
return dp[n];
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 dp[n] = answer;
}
void canBeConverted( int n)
{
memset (dp, -1, sizeof (dp));
if (func(n))
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int N = 13;
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;
}
|
Java
import java.util.*;
class GFG{
static HashSet<Integer> kPowKform =
new HashSet<>();
static int []dp = new int [ 100005 ];
static int func( int n)
{
if (n <= 0 )
return 0 ;
if (kPowKform.contains(n))
return 1 ;
if (dp[n] != - 1 )
return dp[n];
int answer = 0 ;
int x = n;
while (x > 0 )
{
int d = x % 10 ;
if (d != 0 )
{
if (func(n - d * d) != 0 )
{
answer = 1 ;
break ;
}
}
x /= 10 ;
}
return dp[n] = answer;
}
static void canBeConverted( int n)
{
for ( int i = 0 ; i < n; i++)
dp[i] = - 1 ;
if (func(n) == 0 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int N = 13 ;
for ( int i = 1 ; i <= 8 ; i++)
{
int val = 1 ;
for ( int j = 1 ; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
}
}
|
Python3
kPowKform = dict ()
def func(n, dp):
global kPowKform
if (n < = 0 ):
return 0
if (n in kPowKform):
return 1
if (dp[n] ! = - 1 ):
return dp[n]
answer = 0
x = n
while (x > 0 ):
d = x % 10
if (d ! = 0 ):
if (func(n - d * d, dp)):
answer = 1
break
x / / = 10
dp[n] = answer
return answer
def canBeConverted(n):
dp = [ - 1 for i in range ( 10001 )]
if (func(n, dp)):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 13
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 , i + 1 ):
val * = i
kPowKform[val] = 1
canBeConverted(N)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static HashSet< int > kPowKform = new HashSet< int >();
static int []dp = new int [100005];
static int func( int n)
{
if (n <= 0)
return 0;
if (kPowKform.Contains(n))
return 1;
if (dp[n] != -1)
return dp[n];
int answer = 0;
int x = n;
while (x > 0)
{
int d = x % 10;
if (d != 0)
{
if (func(n - d * d) != 0)
{
answer = 1;
break ;
}
}
x /= 10;
}
dp[n] = answer;
return answer;
}
static void canBeConverted( int n)
{
Array.Fill(dp, -1);
if (func(n) != 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main( string [] args)
{
int N = 13;
for ( int i = 1; i <= 8; i++)
{
int val = 1;
for ( int j = 1; j <= i; j++)
val *= i;
kPowKform.Add(val);
}
canBeConverted(N);
}
}
|
Javascript
<script>
var kPowKform = new Set();
var dp = Array(100005);
function func(n)
{
if (n <= 0)
return 0;
if (kPowKform.has(n))
return 1;
if (dp[n] != -1)
return dp[n];
var answer = 0;
var x = n;
while (x > 0) {
var d = x % 10;
if (d != 0) {
if (func(n - d * d)) {
answer = 1;
break ;
}
}
x /= 10;
}
return dp[n] = answer;
}
function canBeConverted(n)
{
dp = Array(100005).fill(-1);
if (func(n))
document.write( "Yes" );
else
document.write( "No" );
}
var N = 13;
for ( var i = 1; i <= 8; i++) {
var val = 1;
for ( var j = 1; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
</script>
|
Time Complexity: O(D * N), where D is the number of digits in N.
Efficient approach: Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memorization(top-down) because memorization method needs extra stack space of recursion calls.
Steps to solve this problem :
- Initialize the dp table with n+1 elements and set all elements to 0 using the memset function.
- Use another loop to solve subproblems in a bottom-up manner. For each value i from 1 to n:
- Check if i is of the form k^k. If yes, set dp[i] to 1 and continue to the next iteration.
- If i is not of the form k^k, iterate through each digit of i using a while loop. For each digit d, check if it is possible to obtain a number of the given form by subtracting d*d from i and checking the corresponding value in the dp table.
- If it is possible to obtain a number of the given form, set found to true and break out of the while loop.
- Store the value of found in dp[i].
- Print the final solution. If dp[n] is equal to 1, print “Yes“, else print “No“.
Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
void canBeConverted( int n)
{
int dp[n+1];
memset (dp, 0, sizeof (dp));
unordered_set< int > kPowKform;
for ( int i = 1; i <= 8; i++) {
int val = 1;
for ( int j = 1; j <= i; j++)
val *= i;
kPowKform.insert(val);
}
dp[0] = 0;
for ( int i = 1; i <= n; i++) {
if (kPowKform.count(i)) {
dp[i] = 1;
continue ;
}
int x = i;
bool found = false ;
while (x > 0) {
int d = x % 10;
if (d != 0) {
if (dp[i - d*d] == 1) {
found = true ;
break ;
}
}
x /= 10;
}
dp[i] = found;
}
if (dp[n] == 1)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int n = 13;
canBeConverted(n);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static void canBeConverted( int n)
{
boolean [] dp = new boolean [n + 1 ];
Arrays.fill(dp, false );
Set<Integer> kPowKform = new HashSet<Integer>();
for ( int i = 1 ; i <= 8 ; i++) {
int val = 1 ;
for ( int j = 1 ; j <= i; j++) {
val *= i;
}
kPowKform.add(val);
}
dp[ 0 ] = true ;
for ( int i = 1 ; i <= n; i++)
{
if (kPowKform.contains(i)) {
dp[i] = true ;
continue ;
}
int x = i;
boolean found = false ;
while (x > 0 ) {
int d = x % 10 ;
if (d != 0 )
{
if (i - d * d >= 0 && dp[i - d * d]) {
found = true ;
break ;
}
}
x /= 10 ;
}
dp[i] = found;
}
if (dp[n]) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
public static void main(String[] args) {
int n = 13 ;
canBeConverted(n);
}
}
|
Python
def canBeConverted(n):
dp = [ 0 ] * (n + 1 )
kPowKform = set ()
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 , i + 1 ):
val * = i
kPowKform.add(val)
dp[ 0 ] = 0
for i in range ( 1 , n + 1 ):
if i in kPowKform:
dp[i] = 1
continue
x = i
found = False
while x > 0 :
d = x % 10
if d ! = 0 :
if i - d * d > = 0 and dp[i - d * d] = = 1 :
found = True
break
x / / = 10
dp[i] = found
if dp[n] = = 1 :
print ( "Yes" )
else :
print ( "No" )
n = 13
canBeConverted(n)
|
C#
using System;
using System.Collections.Generic;
class MainClass
{
public static void canBeConverted( int n)
{
bool [] dp = new bool [n + 1];
Array.Fill(dp, false );
HashSet < int > kPowKform = new HashSet < int > ();
for ( int i = 1; i <= 8; i++) {
int val = 1;
for ( int j = 1; j <= i; j++) {
val *= i;
}
kPowKform.Add(val);
}
dp[0] = true ;
for ( int i = 1; i <= n; i++) {
if (kPowKform.Contains(i)) {
dp[i] = true ;
continue ;
}
int x = i;
bool found = false ;
while (x > 0) {
int d = x % 10;
if (d != 0) {
if (i - d * d >= 0 && dp[i - d * d]) {
found = true ;
break ;
}
}
x /= 10;
}
dp[i] = found;
}
if (dp[n]) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
}
public static void Main( string [] args) {
int n = 13;
canBeConverted(n);
}
}
|
Javascript
function canBeConverted(n) {
let dp = new Array(n + 1).fill(0);
let kPowKform = new Set();
for (let i = 1; i <= 8; i++) {
let val = 1;
for (let j = 1; j <= i; j++)
val *= i;
kPowKform.add(val);
}
dp[0] = 0;
for (let i = 1; i <= n; i++) {
if (kPowKform.has(i)) {
dp[i] = 1;
continue ;
}
let x = i;
let found = false ;
while (x > 0) {
let d = x % 10;
if (d != 0) {
if (dp[i - d * d] == 1) {
found = true ;
break ;
}
}
x = Math.floor(x / 10);
}
dp[i] = found;
}
if (dp[n] == 1)
console.log( "Yes" );
else
console.log( "No" );
}
let n = 13;
canBeConverted(n);
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...