Find the Largest Cube formed by Deleting minimum Digits from a number | Python3 code to implement maximum perfect cube formed after deleting minimum digits ; Returns vector of Pre Processed perfect cubes ; convert the cube to string and push into preProcessedCubes vector ; 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 ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
import math as mt NEW_LINE def preProcess ( n ) : NEW_LINE INDENT preProcessedCubes = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( 1. / 3. ) ) ) : NEW_LINE INDENT iThCube = i ** 3 NEW_LINE cubeString = str ( iThCube ) NEW_LINE preProcessedCubes . append ( cubeString ) NEW_LINE DEDENT return preProcessedCubes NEW_LINE DEDENT def findLargestCubeUtil ( num , preProcessedCubes ) : NEW_LINE INDENT preProcessedCubes = preProcessedCubes [ : : - 1 ] NEW_LINE totalCubes = len ( preProcessedCubes ) NEW_LINE for i in range ( totalCubes ) : NEW_LINE INDENT currCube = preProcessedCubes [ i ] NEW_LINE digitsInCube = len ( currCube ) NEW_LINE index = 0 NEW_LINE digitsInNumber = len ( num ) NEW_LINE for j in range ( digitsInNumber ) : NEW_LINE INDENT if ( num [ j ] == currCube [ index ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT if ( digitsInCube == index ) : NEW_LINE INDENT return currCube NEW_LINE DEDENT DEDENT DEDENT return " Not ▁ Possible " NEW_LINE DEDENT def findLargestCube ( n ) : NEW_LINE INDENT preProcessedCubes = preProcess ( n ) NEW_LINE num = str ( n ) NEW_LINE ans = findLargestCubeUtil ( num , preProcessedCubes ) NEW_LINE print ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from " , n , " is " , ans ) NEW_LINE DEDENT n = 4125 NEW_LINE findLargestCube ( n ) NEW_LINE n = 876 NEW_LINE findLargestCube ( n ) NEW_LINE