Find the Largest Cube formed by Deleting minimum Digits from a number in Python

Suppose we have a number N, we have to determine the largest perfect cube that can be generated by removing minimum digits (possibly 0) from the number. We can delete any digit from the given number to reach the target. As we know a number N is called a perfect cube if N = M³ for some integer M.

So, if the input is like 806, then the output will be 8, as we can delete 0 and 6 from the number, then we will get 8, which is perfect cube of 2.

Algorithm

To solve this, we will follow these steps ?

  • Generate all perfect cubes up to the cube root of the given number
  • Sort cubes in descending order to find the largest one first
  • For each cube, check if its digits can be found as a subsequence in the original number
  • Return the first (largest) cube that matches

Implementation

Step 1: Generate Perfect Cubes

First, we create a function to generate all perfect cubes up to the cube root of our number ?

import math

def preProcess(n):
    temp_cubes = []
    for i in range(1, math.ceil(n**(1. / 3)) + 1):
        cube = i**3
        cubeString = str(cube)
        temp_cubes.append(cubeString)
    return temp_cubes

# Test the function
n = 806
cubes = preProcess(n)
print("Generated cubes:", cubes[:10])  # Show first 10 cubes
Generated cubes: ['1', '8', '27', '64', '125', '216', '343', '512', '729']

Step 2: Check Subsequence Match

Next, we check if digits of a cube form a subsequence in the original number ?

def solve(num, temp_cubes):
    # Reverse to check largest cubes first
    temp_cubes = temp_cubes[::-1]
    totalCubes = len(temp_cubes)
    
    for i in range(totalCubes):
        temp = temp_cubes[i]
        digitsInCube = len(temp)
        index = 0
        digitsInNumber = len(num)
        
        for j in range(digitsInNumber):
            if num[j] == temp[index]:
                index += 1
            if digitsInCube == index:
                return temp
    
    return "Not Possible"

# Test with example
num = "806"
cubes = ['1', '8', '27', '64', '125', '216', '343', '512', '729']
result = solve(num, cubes)
print("Largest cube found:", result)
Largest cube found: 8

Complete Solution

Here's the complete implementation ?

import math

def preProcess(n):
    temp_cubes = []
    for i in range(1, math.ceil(n**(1. / 3)) + 1):
        cube = i**3
        cubeString = str(cube)
        temp_cubes.append(cubeString)
    return temp_cubes

def solve(num, temp_cubes):
    temp_cubes = temp_cubes[::-1]
    totalCubes = len(temp_cubes)
    
    for i in range(totalCubes):
        temp = temp_cubes[i]
        digitsInCube = len(temp)
        index = 0
        digitsInNumber = len(num)
        
        for j in range(digitsInNumber):
            if num[j] == temp[index]:
                index += 1
            if digitsInCube == index:
                return temp
    
    return "Not Possible"

def getLargestCube(n):
    temp_cubes = preProcess(n)
    num = str(n)
    ans = solve(num, temp_cubes)
    return ans

# Test with examples
test_cases = [806, 4125, 9, 100]

for n in test_cases:
    result = getLargestCube(n)
    print(f"Input: {n}, Largest cube: {result}")
Input: 806, Largest cube: 8
Input: 4125, Largest cube: 125
Input: 9, Largest cube: Not Possible
Input: 100, Largest cube: 1

How It Works

The algorithm works by:

  • Generating cubes: Create all perfect cubes up to ?n
  • Sorting: Process cubes in descending order to find the largest first
  • Subsequence matching: Check if cube digits appear in order within the original number
  • Early termination: Return the first match found (which is the largest)

Time Complexity

The time complexity is O(?n × m), where n is the input number and m is the number of digits in n. The space complexity is O(?n) for storing the cube strings.

Conclusion

This algorithm efficiently finds the largest perfect cube by generating all possible cubes and checking subsequence matches. The key insight is processing cubes in descending order to find the largest valid cube first.

Updated on: 2026-03-25T10:02:38+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements