
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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^3 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, this is perfect cube of 2.
To solve this, we will follow these steps −
- Define a function preProcess() . This will take n
- temp_cubes := a new list
- for i in range 1 to ceiling of n^(1/3), do
- cube = i^3
- cubeString := cube as string
- insert cubeString at the end of temp_cubes
- return temp_cubes
- Define a function solve() . This will take num,temp_cubes
- reverse temp_cubes
- totalCubes := size of temp_cubes
- for i in range 0 to totalCubes, do
- temp := temp_cubes[i]
- digitsInCube := size of temp
- index := 0
- digitsInNumber := size of num
- for j in range 0 to digitsInNumber, do
- if num[j] is same as temp[index], then
- index := index + 1
- if digitsInCube is same as index, then
- return temp
- if num[j] is same as temp[index], then
- return "Not Possible"
- From the method do the following −
- temp_cubes := preProcess(n)
- num := n as string
- ans := solve(num, temp_cubes)
- return ans
Example
Let us see the following implementation to get better understanding −
import math def preProcess(n): temp_cubes = list() for i in range(1, math.ceil(n**(1. / 3.))): 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 n = 806 print(getLargestCube(n) )
Input
806
Output
8
- Related Articles
- Find the Largest Cube formed by Deleting minimum Digits from a number in C++
- Find the largest number that can be formed with the given digits in C++
- Find all strings formed from characters mapped to digits of a number in Python
- By what smallest number should we multiply 26244 so that the number becomes a perfect cube? Find the cube root of the number formed.
- Find the Largest number with given number of digits and sum of digits in C++
- By what smallest number should we multiply 53240, so that the product becomes a perfect cube. Find the cube root of the number formed.
- Find the difference between the smallest number of 7 digits and the largest number of digits.
- Finding the just bigger number formed by same digits in JavaScript
- What is the difference between the largest and smallest numbers formed by the digits \( 4,6,9,8 ? \)
- Find the difference between the smallest number of 7 digits and the largest number of 4 digits.
- Find the largest after deleting the given elements in C++
- Program to find minimum digits sum of deleted digits in Python
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find minimum amplitude after deleting K elements in Python
- Minimum sum of two numbers formed from digits of an array in C++

Advertisements