Find the Largest Cube formed by Deleting minimum Digits from a number | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > preProcess ( long long int n ) { vector < string > preProcessedCubes ; for ( int i = 1 ; i * i * i <= n ; i ++ ) { long long int iThCube = i * i * i ; string cubeString = to_string ( iThCube ) ; preProcessedCubes . push_back ( cubeString ) ; } return preProcessedCubes ; } string findLargestCubeUtil ( string num , vector < string > preProcessedCubes ) { reverse ( preProcessedCubes . begin ( ) , preProcessedCubes . end ( ) ) ; int totalCubes = preProcessedCubes . size ( ) ; for ( int i = 0 ; i < totalCubes ; i ++ ) { string currCube = preProcessedCubes [ i ] ; int digitsInCube = currCube . length ( ) ; int index = 0 ; int digitsInNumber = num . length ( ) ; for ( int j = 0 ; j < digitsInNumber ; j ++ ) { if ( num [ j ] == currCube [ index ] ) index ++ ; if ( digitsInCube == index ) return currCube ; } } return " Not ▁ Possible " ; } void findLargestCube ( long long int n ) { vector < string > preProcessedCubes = preProcess ( n ) ; string num = to_string ( n ) ; string ans = findLargestCubeUtil ( num , preProcessedCubes ) ; cout << " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " << n << " ▁ is ▁ " << ans << endl ; } int main ( ) { long long int n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ; return 0 ; }