Given a number n, the task is to find the largest perfect cube that can be formed by deleting minimum digits(possibly 0) from the number.
X is called a perfect cube if X = Y3 for some Y.
Input : 4125 Output : 125 Explanation 125 = 53. We can form 125 by deleting digit 4 from 4125 Input : 876 Output :8 Explanation 8 = 23. We can form 8 by deleting digits 7 and 6 from 876
We can generate cubes of all numbers till from 1 to N1/3 (We don’t consider 0 as 0 is not considered as a perfect cube). We iterate the cubes from largest to the smallest.
Now if we look at the number n given to us, then we know that this number contains only log(n) + 1 digits, thus we can efficiently approach the problem if we treat this number n as a string hereafter.
While iterating on the perfect cubes, we check if the perfect cube is a subsequence of the number n when its represented as a string.If this is the case then the deletions required for changing the number n to the current perfect cube is:
No of deleted digits = No of digits in number n - Number of digits in current perfect cube
Since we want the largest cube number we traverse the array of preprocessed cubes in reverse order.
# Python3 code to implement maximum perfect
# cube formed after deleting minimum digits
import math as mt
# Returns vector of Pre Processed
# perfect cubes
preProcessedCubes = list()
for i in range(1, mt.ceil(n**(1. / 3.))):
iThCube = i**3
# convert the cube to string and
# push into preProcessedCubes vector
cubeString = str(iThCube)
# Utility function for findLargestCube().
# Returns the Largest cube number that
# can be formed
# reverse the preProcessed cubes so
# that we have the largest cube in
# the beginning of the vector
preProcessedCubes = preProcessedCubes[::-1]
totalCubes = len(preProcessedCubes)
# iterate over all cubes
for i in range(totalCubes):
currCube = preProcessedCubes[i]
digitsInCube = len(currCube)
index = 0
digitsInNumber = len(num)
for j in range(digitsInNumber):
# check if the current digit of the cube
# matches with that of the number num
if (num[j] == currCube[index]):
index += 1
if (digitsInCube == index):
# if control reaches here, the its
# not possible to form a perfect cube
return “Not Possible”
# wrapper for findLargestCubeUtil()
# pre process perfect cubes
preProcessedCubes = preProcess(n)
num = str(n)
ans = findLargestCubeUtil(num, preProcessedCubes)
print(“Largest Cube that can be formed from”,
n, “is”, ans)
# Driver Code
n = 4125
n = 876
# This code is contributed
# by mohit kumar 29
Largest Cube that can be formed from 4125 is 125 Largest Cube that can be formed from 876 is 8
Time Complexity of the above algorithm is O(N1/3log(N) log(N) is due to the fact that the number of digits in N are Log(N) + 1.
- Find maximum number that can be formed using digits of a given number
- Find the largest after deleting the given elements
- Find the k largest numbers after deleting the given elements
- Find the Largest number with given number of digits and sum of digits
- Find largest number smaller than N with same set of digits
- Minimum sum of two numbers formed from digits of an array
- Largest perfect cube number in an Array
- Largest number in an array that is not a perfect cube
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Greatest number less than equal to B that can be formed from the digits of A
- Recursive sum of digits of a number formed by repeated appends
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find largest sum of digits in all divisors of n
- Minimum number of consecutive sequences that can be formed in an array
- Largest number not greater than N all the digits of which are odd
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.
Improved By : mohit kumar 29