Finding the Missing digit in the Largest Perfect Cube Last Updated : 29 Nov, 2023 Comments Improve Suggest changes Like Article Like Report Given a perfect cube number with one missing digit represented by an underscore _, the task is to find the missing digit that when inserted, creates the largest perfect cube. Examples: Input: "219_"Output: 7Explanation: The missing digit 7 makes the largest perfect cube, 2197. Input: 15_25Output: 6Explanation: The missing digit 6 makes the largest perfect square, 15625. Approach: The problem can be solved using the following approach: Iterate through all possible digits in reverse order (9 to 0) and replace the underscore with the current digit, calculate the cube root of the resulting number, and check if it is a perfect cube. We have iterated in reverse order (9 to 0) because we want to have the largest perfect cube. Steps to solve the problem: Initialize variable missing_digit to -1.Iterate through digits from 9 to 0.For each digit, replace the underscore in the input number with the current digit.Calculate the cube root of the modified number.If the cube root is an integer, return the digit.Below is the implementation of the approach: C++ #include <cmath> #include <iostream> using namespace std; int findMissingDigitCube(string input_str) { int missing_digit = -1; // Iterate through digits from 9 to 0 for (int digit = 9; digit >= 0; digit--) { // Replace the underscore with the current digit string modified_str = input_str; size_t pos = modified_str.find('_'); if (pos != string::npos) { modified_str.replace(pos, 1, to_string(digit)); } // Calculate the cube root of the modified number double cube_root = cbrt(stoi(modified_str)); // Check if the cube root is an integer if (abs(round(cube_root) - cube_root) < 1e-9) { missing_digit = digit; break; } } return missing_digit; } // Driver COde int main() { cout << findMissingDigitCube("219_") << endl; // Output: 7 cout << findMissingDigitCube("15_25") << endl; // Output: 6 return 0; } Java import java.util.regex.Matcher; import java.util.regex.Pattern; public class MissingDigitCube { public static int findMissingDigitCube(String inputStr) { int missingDigit = -1; // Iterate through digits from 9 to 0 for (int digit = 9; digit >= 0; digit--) { // Replace the underscore with the current digit String modifiedStr = inputStr.replaceAll( "_", Integer.toString(digit)); // Calculate the cube root of the modified // number double cubeRoot = Math.cbrt(Integer.parseInt(modifiedStr)); // Check if the cube root is an integer if (Math.abs(Math.round(cubeRoot) - cubeRoot) < 1e-9) { missingDigit = digit; break; } } return missingDigit; } public static void main(String[] args) { System.out.println( findMissingDigitCube("219_")); // Output: 7 System.out.println( findMissingDigitCube("15_25")); // Output: 6 } } Python3 import math def find_missing_digit_cube(input_str): missing_digit = -1 # Iterate through digits from 9 to 0 for digit in range(9, -1, -1): # Replace the underscore with the current digit modified_str = input_str.replace('_', str(digit)) # Calculate the cube root of the modified number cube_root = round(int(modified_str) ** (1/3), 9) # Use rounding to avoid floating-point precision issues # Check if the cube root is an integer if abs(cube_root - round(cube_root)) < 1e-9: # Tolerance for floating-point comparison missing_digit = digit break return missing_digit # Driver code print(find_missing_digit_cube("219_")) # Output: 7 print(find_missing_digit_cube("15_25")) # Output: 6 C# using System; public class MissingDigitCube { public static int FindMissingDigitCube(string inputStr) { int missingDigit = -1; // Iterate through digits from 9 to 0 for (int digit = 9; digit >= 0; digit--) { // Replace the underscore with the current digit string modifiedStr = inputStr.Replace("_", digit.ToString()); // Calculate the cube root of the modified number double cubeRoot = Math.Cbrt(int.Parse(modifiedStr)); // Check if the cube root is an integer if (Math.Abs(Math.Round(cubeRoot) - cubeRoot) < 1e-9) { missingDigit = digit; break; } } return missingDigit; } public static void Main(string[] args) { Console.WriteLine(FindMissingDigitCube("219_")); // Output: 7 Console.WriteLine(FindMissingDigitCube("15_25")); // Output: 6 } } JavaScript function findMissingDigitCube(inputStr) { let missingDigit = -1; // Iterate through digits from 9 to 0 for (let digit = 9; digit >= 0; digit--) { // Replace the underscore with the current digit let modifiedStr = inputStr; let pos = modifiedStr.indexOf('_'); if (pos !== -1) { modifiedStr = modifiedStr.substring(0, pos) + digit + modifiedStr.substring(pos + 1); } // Calculate the cube root of the modified number let cubeRoot = Math.cbrt(parseInt(modifiedStr)); // Check if the cube root is an integer if (Math.abs(Math.round(cubeRoot) - cubeRoot) < 1e-9) { missingDigit = digit; break; } } return missingDigit; } // Driver Code console.log(findMissingDigitCube("219_")); // Output: 7 console.log(findMissingDigitCube("15_25")); // Output: 6 Output7 6Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Finding the Missing digit in the Largest Perfect Cube Anonymous Improve Article Tags : Mathematical Geeks Premier League DSA Maths Practice Tags : Mathematical Similar Reads Find the Largest N digit perfect square number in Base B Given two integers N and B, the task is to find the largest N digit numbers of Base B which is a perfect square.Examples: Input: N = 2, B = 10 Output: 81 Explanation: 81 is the largest 2-digit perfect square in base 10.Input: N = 1, B = 8 Output: 4 Explanation: 4 is the largest 1 digit Octal number 9 min read Find the missing digit in given product of large positive integers Given two large integers in form of strings A and B and their product also in form of string C such that one digit of the product is replaced with X, the task is to find the replaced digit in the product C. Examples: Input: A = 51840, B = 273581, C = 1418243x040Output: 9Explanation:The product of in 11 min read Largest Pefect Cube by Deleting Minimum Digits Given a number n, the task is to find the largest perfect cube that can be formed by deleting the fewest number of digits (possibly 0 digits). A number x is a perfect cube if it can be written as x = y3 for some integer y.You can delete any digits from the given number n, but you cannot rearrange th 8 min read Smallest and Largest N-digit perfect cubes Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect cubes.Examples: Input: N = 2 Output: 27 64 27 and 64 are the smallest and the largest 2-digit numbers which are also perfect cubes.Input: N = 3 Output: 125 729 Approach: For increasing values 3 min read Find the largest N digit multiple of N Given a number N, the task is to find the largest N-digit multiple of N. Examples: Input: N = 2 Output: 98 Explanation: 98 is the largest multiple of 2 and is of 2 digits.Input: N = 3 Output: 999 Explanation: 999 is the largest multiple of 3 and is of 3 digits. Approach: The idea is to make an obser 3 min read Largest N digit number divisible by given three numbers Given four integers x, y, z, and n, the task is to find the largest n digit number which is divisible by x, y, and z. Examples: Input: x = 2, y = 3, z = 5, n = 4 Output: 9990 9990 is the largest 4-digit number which is divisible by 2, 3 and 5.Input: x = 3, y = 23, z = 6, n = 2 Output: Not possible A 9 min read Largest number in an array that is not a perfect cube Given an array of n integers. The task is to find the largest number which is not a perfect cube. Print -1 if there is no number that is a perfect cube. Examples: Input: arr[] = {16, 8, 25, 2, 3, 10} Output: 25 25 is the largest number that is not a perfect cube. Input: arr[] = {36, 64, 10, 16, 29, 8 min read Smallest and Largest N-digit perfect squares Given an integer N, the task is to find the smallest and the largest N digit numbers which are also perfect squares.Examples: Input: N = 2 Output: 16 81 16 and 18 are the smallest and the largest 2-digit perfect squares.Input: N = 3 Output: 100 961 Approach: For increasing values of N starting from 3 min read Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v 13 min read Largest cube that can be inscribed within the sphere Given here is a sphere of radius r, the task is to find the side of the largest cube that can fit inside in it.Examples: Input: r = 8 Output: 9.2376 Input: r = 5 Output: 5.7735 Approach: Side of the cube = a Radius of the sphere = r From the diagonal, it is clear that, diagonal of the cube = diamete 3 min read Like