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 required form then print “Yes” otherwise print “No”.
Input: N = 13
For integer 13 choose d = 3 : N = 13 – 32 = 4, 4 is of the form 22. Hence, the output is 4.
Input: N = 90
It is not possible to express the number 90 in required form.
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:
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 memoize the recursive approach using a cache or memorization table.
Below is the implementation of the above approach:
Time Complexity: O(D * N), where D is the number of digits in N.
- Check if one string can be converted to other using given operation
- Check if one string can be converted to another
- Check if array can be converted into strictly decreasing sequence
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Check if the last element of array is even or odd after performing a operation p times
- Check if possible to cross the matrix with given power
- Check if the given 2-D points form T-shape or not
- Check if it is possible to form string B from A under the given constraints
- Check if K distinct array elements form an odd sum
- Find the sum of power of bit count raised to the power B
- Check if the number is a Prime power number
- Check if a string is a scrambled form of another string
- Maximum possible array sum after performing the given operation
- Sum of the updated array after performing the given operation
- Find average of two numbers using bit operation
- Count the numbers which can convert N to 1 using given operation
- Make all elements of an array equal with the given operation
- Number of ways to color N-K blocks using given operation
- Minimum number of given operation required to convert n to m
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.