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++ implementation to Check whether a given // number N can be converted to the form K // power K by the given operation #include <bits/stdc++.h> using namespace std;
unordered_set< int > kPowKform;
// Function to check if N can // be converted to K power K int func( int n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.count(n))
return 1;
int answer = 0;
int x = n;
// Iterate through each digit of n
while (x > 0) {
int d = x % 10;
if (d != 0) {
// Check if it is possible to
// obtain number of given form
if (func(n - d * d)) {
answer = 1;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Return the result
return answer;
} // Function to check the above method void canBeConverted( int n)
{ // Check if conversion if possible
if (func(n))
cout << "Yes" ;
else
cout << "No" ;
} // Driver code int main()
{ int N = 90;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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 implementation to // Check whether a given // number N can be converted // to the form K power K by // the given operation import java.util.*;
class GFG{
static HashSet<Integer> kPowKform =
new HashSet<Integer>();
// Function to check if N can // be converted to K power K static int func( int n)
{ if (n <= 0 )
return 0 ;
// Check if n is of the form k^k
if (kPowKform.contains(n))
return 1 ;
int answer = 0 ;
int x = n;
// Iterate through
// each digit of n
while (x > 0 )
{
int d = x % 10 ;
if (d != 0 )
{
// Check if it is possible to
// obtain number of given form
if (func(n - d * d) == 1 )
{
answer = 1 ;
break ;
}
}
// Reduce the number each time
x /= 10 ;
}
// Return the result
return answer;
} // Function to check the above method static void canBeConverted( int n)
{ // Check if conversion if possible
if (func(n) == 1 )
System.out.print( "Yes" );
else
System.out.print( "No" );
} // Driver code public static void main(String[] args)
{ int N = 90 ;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
for ( int i = 1 ; i <= 8 ; i++)
{
int val = 1 ;
for ( int j = 1 ; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
} } // This code is contributed by Rajput-Ji |
# Python3 implementation to Check whether a given # number N can be converted to the form K # power K by the given operation kPowKform = dict ()
# Function to check if N can # be converted to K power K def func(n):
global kPowKform
if (n < = 0 ):
return 0
# Check if n is of the form k^k
if (n in kPowKform):
return 1
answer = 0
x = n
# Iterate through each digit of n
while (x > 0 ):
d = x % 10
if (d ! = 0 ):
# Check if it is possible to
# obtain number of given form
if (func(n - d * d)):
answer = 1
break
# Reduce the number each time
x / / = 10
# Return the result
return answer
# Function to check the above method def canBeConverted(n):
# Check if conversion if possible
if (func(n)):
print ( "Yes" )
else :
print ( "No" )
# Driver code if __name__ = = '__main__' :
N = 90
# Pre store K power K form of numbers
# Loop till 8, because 8^8 > 10^7
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 ,i + 1 ):
val * = i
kPowKform[val] = 1
canBeConverted(N)
# This code is contributed by mohit kumar 29 |
// C# implementation to check whether a given // number N can be converted to the form K // power K by the given operation using System;
using System.Collections.Generic;
class GFG{
static SortedSet< int > kPowKform = new SortedSet< int >();
// Function to check if N can // be converted to K power K static int func( int n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.Contains(n))
return 1;
int answer = 0;
int x = n;
// Iterate through each digit of n
while (x > 0)
{
int d = x % 10;
if (d != 0)
{
// Check if it is possible to
// obtain number of given form
if (func(n - d * d) == 1)
{
answer = 1;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Return the result
return answer;
} // Function to check the above method static void canBeConverted( int n)
{ // Check if conversion if possible
if (func(n) == 1)
Console.Write( "Yes" );
else
Console.Write( "No" );
} // Driver code public static void Main()
{ int N = 90;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
for ( int i = 1; i <= 8; i++)
{
int val = 1;
for ( int j = 1; j <= i; j++)
val *= i;
kPowKform.Add(val);
}
canBeConverted(N);
} } // This code is contributed by sanjoy_62 |
<script> // Javascript implementation to Check whether a given // number N can be converted to the form K // power K by the given operation var kPowKform = new Set();
// Function to check if N can // be converted to K power K function func(n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.has(n))
return 1;
var answer = 0;
var x = n;
// Iterate through each digit of n
while (x > 0)
{
var d = x % 10;
if (d != 0)
{
// Check if it is possible to
// obtain number of given form
if (func(n - d * d)) {
answer = 1;
break ;
}
}
// Reduce the number each time
x = parseInt(x/10);
}
// Return the result
return answer;
} // Function to check the above method function canBeConverted(n)
{ // Check if conversion if possible
if (func(n))
document.write( "Yes" );
else
document.write( "No" );
} // Driver code var N = 90;
// Pre store K power K form of numbers // Loop till 8, because 8^8 > 10^7 for ( var i = 1; i <= 8; i++) {
var val = 1;
for ( var j = 1; j <= i; j++)
val *= i;
kPowKform.add(val);
} canBeConverted(N); // This code is contributed by noob2000. </script> |
No
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++ implementation to Check whether a given // number N can be converted to the form K // power K by the given operation #include <bits/stdc++.h> using namespace std;
unordered_set< int > kPowKform;
int dp[100005];
// Function to check if a number is converatable int func( int n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.count(n))
return 1;
// Check if the subproblem has been solved before
if (dp[n] != -1)
return dp[n];
int answer = 0;
int x = n;
// Iterate through each digit of n
while (x > 0) {
int d = x % 10;
if (d != 0) {
// Check if it is possible to
// obtain number of given form
if (func(n - d * d)) {
answer = 1;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Store and return the
// answer to this subproblem
return dp[n] = answer;
} // Function to check the above method void canBeConverted( int n)
{ // Initialise the dp table
memset (dp, -1, sizeof (dp));
// Check if conversion if possible
if (func(n))
cout << "Yes" ;
else
cout << "No" ;
} // Driver code int main()
{ int N = 13;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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 implementation to // Check whether a given // number N can be converted // to the form K power K by // the given operation import java.util.*;
class GFG{
static HashSet<Integer> kPowKform =
new HashSet<>();
static int []dp = new int [ 100005 ];
// Function to check if // a number is converatable static int func( int n)
{ if (n <= 0 )
return 0 ;
// Check if n is of the form k^k
if (kPowKform.contains(n))
return 1 ;
// Check if the subproblem
// has been solved before
if (dp[n] != - 1 )
return dp[n];
int answer = 0 ;
int x = n;
// Iterate through each digit of n
while (x > 0 )
{
int d = x % 10 ;
if (d != 0 )
{
// Check if it is possible to
// obtain number of given form
if (func(n - d * d) != 0 )
{
answer = 1 ;
break ;
}
}
// Reduce the number
// each time
x /= 10 ;
}
// Store and return the
// answer to this subproblem
return dp[n] = answer;
} // Function to check the above method static void canBeConverted( int n)
{ // Initialise the dp table
for ( int i = 0 ; i < n; i++)
dp[i] = - 1 ;
// Check if conversion if possible
if (func(n) == 0 )
System.out.print( "Yes" );
else
System.out.print( "No" );
} // Driver code public static void main(String[] args)
{ int N = 13 ;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
for ( int i = 1 ; i <= 8 ; i++)
{
int val = 1 ;
for ( int j = 1 ; j <= i; j++)
val *= i;
kPowKform.add(val);
}
canBeConverted(N);
} } // This code is contributed by Rajput-Ji |
# Python3 implementation to check whether # a given number N can be converted to # the form K power K by the given operation kPowKform = dict ()
# Function to check if N can # be converted to K power K def func(n, dp):
global kPowKform
if (n < = 0 ):
return 0
# Check if n is of the form k^k
if (n in kPowKform):
return 1
if (dp[n] ! = - 1 ):
return dp[n]
answer = 0
x = n
# Iterate through each digit of n
while (x > 0 ):
d = x % 10
if (d ! = 0 ):
# Check if it is possible to
# obtain number of given form
if (func(n - d * d, dp)):
answer = 1
break
# Reduce the number each time
x / / = 10
dp[n] = answer
# Return the result
return answer
# Function to check the above method def canBeConverted(n):
dp = [ - 1 for i in range ( 10001 )]
# Check if conversion if possible
if (func(n, dp)):
print ( "Yes" )
else :
print ( "No" )
# Driver code if __name__ = = '__main__' :
N = 13
# Pre store K power K form of
# numbers Loop till 8, because
# 8^8 > 10^7
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 , i + 1 ):
val * = i
kPowKform[val] = 1
canBeConverted(N)
# This code is contributed by grand_master |
// C# implementation to check whether a given // number N can be converted to the form K // power K by the given operation using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static HashSet< int > kPowKform = new HashSet< int >();
static int []dp = new int [100005];
// Function to check if a number // is converatable static int func( int n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.Contains(n))
return 1;
// Check if the subproblem has
// been solved before
if (dp[n] != -1)
return dp[n];
int answer = 0;
int x = n;
// Iterate through each digit of n
while (x > 0)
{
int d = x % 10;
if (d != 0)
{
// Check if it is possible to
// obtain number of given form
if (func(n - d * d) != 0)
{
answer = 1;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Store and return the
// answer to this subproblem
dp[n] = answer;
return answer;
} // Function to check the above method static void canBeConverted( int n)
{ // Initialise the dp table
Array.Fill(dp, -1);
// Check if conversion if possible
if (func(n) != 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
} // Driver code public static void Main( string [] args)
{ int N = 13;
// Pre store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
for ( int i = 1; i <= 8; i++)
{
int val = 1;
for ( int j = 1; j <= i; j++)
val *= i;
kPowKform.Add(val);
}
canBeConverted(N);
} } // This code is contributed by rutvik_56 |
<script> // Javascript implementation to Check whether a given // number N can be converted to the form K // power K by the given operation var kPowKform = new Set();
var dp = Array(100005);
// Function to check if a number is converatable function func(n)
{ if (n <= 0)
return 0;
// Check if n is of the form k^k
if (kPowKform.has(n))
return 1;
// Check if the subproblem has been solved before
if (dp[n] != -1)
return dp[n];
var answer = 0;
var x = n;
// Iterate through each digit of n
while (x > 0) {
var d = x % 10;
if (d != 0) {
// Check if it is possible to
// obtain number of given form
if (func(n - d * d)) {
answer = 1;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Store and return the
// answer to this subproblem
return dp[n] = answer;
} // Function to check the above method function canBeConverted(n)
{ // Initialise the dp table
dp = Array(100005).fill(-1);
// Check if conversion if possible
if (func(n))
document.write( "Yes" );
else
document.write( "No" );
} // Driver code var N = 13;
// Pre store K power K form of numbers // Loop till 8, because 8^8 > 10^7 for ( var i = 1; i <= 8; i++) {
var val = 1;
for ( var j = 1; j <= i; j++)
val *= i;
kPowKform.add(val);
} canBeConverted(N); // This code is contributed by famously. </script> |
Yes
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 :
#include <bits/stdc++.h> using namespace std;
// Function to check if a number is convertible void canBeConverted( int n)
{ // Initialize the dp table
int dp[n+1];
memset (dp, 0, sizeof (dp));
// Pre-store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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);
}
// Base case
dp[0] = 0;
// Solve subproblems in a bottom-up manner
for ( int i = 1; i <= n; i++) {
// Check if i is of the form k^k
if (kPowKform.count(i)) {
dp[i] = 1;
continue ;
}
int x = i;
bool found = false ;
// Iterate through each digit of i
while (x > 0) {
int d = x % 10;
if (d != 0) {
// Check if it is possible to obtain number of given form
if (dp[i - d*d] == 1) {
found = true ;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Store the answer to this subproblem
dp[i] = found;
}
// Print the final solution
if (dp[n] == 1)
cout << "Yes" ;
else
cout << "No" ;
} // Driver code int main()
{ int n = 13;
canBeConverted(n);
return 0;
} |
import java.util.*;
public class Main
{ // Function to check if a number is convertible
public static void canBeConverted( int n)
{
// Initialize the dp table
boolean [] dp = new boolean [n + 1 ];
Arrays.fill(dp, false );
// Pre-store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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);
}
// Base case
dp[ 0 ] = true ;
// Solve subproblems in a bottom-up manner
for ( int i = 1 ; i <= n; i++)
{
// Check if i is of the form k^k
if (kPowKform.contains(i)) {
dp[i] = true ;
continue ;
}
int x = i;
boolean found = false ;
// Iterate through each digit of i
while (x > 0 ) {
int d = x % 10 ;
if (d != 0 )
{
// Check if it is possible to obtain number of given form
if (i - d * d >= 0 && dp[i - d * d]) {
found = true ;
break ;
}
}
// Reduce the number each time
x /= 10 ;
}
// Store the answer to this subproblem
dp[i] = found;
}
// Print the final solution
if (dp[n]) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
// Driver code
public static void main(String[] args) {
int n = 13 ;
canBeConverted(n);
}
} |
def canBeConverted(n):
# Initialize the dp table
dp = [ 0 ] * (n + 1 )
# Pre-store K power K form of numbers
# Loop till 8, because 8^8 > 10^7
kPowKform = set ()
for i in range ( 1 , 9 ):
val = 1
for j in range ( 1 , i + 1 ):
val * = i
kPowKform.add(val)
# Base case
dp[ 0 ] = 0
# Solve subproblems in a bottom-up manner
for i in range ( 1 , n + 1 ):
# Check if i is of the form k^k
if i in kPowKform:
dp[i] = 1
continue
x = i
found = False
# Iterate through each digit of i
while x > 0 :
d = x % 10
if d ! = 0 :
# Check if it is possible to obtain number of given form
if i - d * d > = 0 and dp[i - d * d] = = 1 :
found = True
break
# Reduce the number each time
x / / = 10
# Store the answer to this subproblem
dp[i] = found
# Print the final solution
if dp[n] = = 1 :
print ( "Yes" )
else :
print ( "No" )
# Driver code n = 13
canBeConverted(n) |
// C# code addition using System;
using System.Collections.Generic;
class MainClass
{ // Function to check if a number is convertible
public static void canBeConverted( int n)
{
// Initialize the dp table
bool [] dp = new bool [n + 1];
Array.Fill(dp, false );
// Pre-store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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);
}
// Base case
dp[0] = true ;
// Solve subproblems in a bottom-up manner
for ( int i = 1; i <= n; i++) {
// Check if i is of the form k^k
if (kPowKform.Contains(i)) {
dp[i] = true ;
continue ;
}
int x = i;
bool found = false ;
// Iterate through each digit of i
while (x > 0) {
int d = x % 10;
if (d != 0) {
// Check if it is possible to obtain number of given form
if (i - d * d >= 0 && dp[i - d * d]) {
found = true ;
break ;
}
}
// Reduce the number each time
x /= 10;
}
// Store the answer to this subproblem
dp[i] = found;
}
// Print the final solution
if (dp[n]) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
}
// Driver code
public static void Main( string [] args) {
int n = 13;
canBeConverted(n);
}
} // The code is contributed by Nidhi goel. |
// Function to check if a number is convertible function canBeConverted(n) {
// Initialize the dp table
let dp = new Array(n + 1).fill(0);
// Pre-store K power K form of numbers
// Loop till 8, because 8^8 > 10^7
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);
}
// Base case
dp[0] = 0;
// Solve subproblems in a bottom-up manner
for (let i = 1; i <= n; i++) {
// Check if i is of the form k^k
if (kPowKform.has(i)) {
dp[i] = 1;
continue ;
}
let x = i;
let found = false ;
// Iterate through each digit of i
while (x > 0) {
let d = x % 10;
if (d != 0) {
// Check if it is possible to obtain number of given form
if (dp[i - d * d] == 1) {
found = true ;
break ;
}
}
// Reduce the number each time
x = Math.floor(x / 10);
}
// Store the answer to this subproblem
dp[i] = found;
}
// Print the final solution
if (dp[n] == 1)
console.log( "Yes" );
else
console.log( "No" );
} // Driver code let n = 13; canBeConverted(n); |
Yes
Time Complexity: O(NlogN)
Auxiliary Space: O(N)