Find the Largest Cube formed by Deleting minimum Digits from a number | 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
function preProcess ( n ) { let preProcessedCubes = [ ] ; for ( let i = 1 ; i * i * i <= n ; i ++ ) { let iThCube = i * i * i ; let cubeString = ( iThCube ) . toString ( ) ; preProcessedCubes . push ( cubeString ) ; } return preProcessedCubes ; } function findLargestCubeUtil ( num , preProcessedCubes ) { ( preProcessedCubes ) . reverse ( ) ; let totalCubes = preProcessedCubes . length ; for ( let i = 0 ; i < totalCubes ; i ++ ) { let currCube = preProcessedCubes [ i ] ; let digitsInCube = currCube . length ; let index = 0 ; let digitsInNumber = num . length ; for ( let j = 0 ; j < digitsInNumber ; j ++ ) { if ( num [ j ] == currCube [ index ] ) { index ++ ; } if ( digitsInCube == index ) { return currCube ; } } } return " " ; } function findLargestCube ( n ) { let preProcessedCubes = preProcess ( n ) ; let num = ( n ) . toString ( ) ; let ans = findLargestCubeUtil ( num , preProcessedCubes ) ; document . write ( " " + n + " " + ans + " " ) ; } let n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ;