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 the digits. The goal is to find the largest perfect cube that can be formed with the remaining digits.
Examples:
Input: 4125
Output: 125
Explanation: Deleting '4' from 4125 forms 125, the largest perfect cube.Input: 876
Output: 8
Explanation: Deleting '7' and '6' from 876 forms 8, the largest perfect cube.
Table of Content
[Naive Approach] Checking all subsequences - O(2^log nlog n) time and O(log n) space
The idea is to generate all possible subsequences of the given number, check if they are perfect cubes, and then keep track of the largest one. This will be inefficient for large numbers due to the number of subsequences.
#include <bits/stdc++.h>
using namespace std;
// Check if a number is a perfect cube
bool isCube(int x) {
int cbr = round(cbrt(x));
return cbr * cbr * cbr == x;
}
// Generate all subsequences and check for largest perfect cube
int largestCube(string n) {
int maxC = 0;
int len = n.size();
// Generate all subsequences
for (int i = 1; i < (1 << len); i++) {
string sub = "";
for (int j = 0; j < len; j++) {
if (i & (1 << j)) {
sub.push_back(n[j]);
}
}
int num = stoi(sub);
if (isCube(num)) {
maxC = max(maxC, num);
}
}
return maxC;
}
int main() {
string n = "4125";
cout << largestCube(n) << endl;
return 0;
}
class GfG {
// Check if a number is a perfect cube
public static boolean isCube(int x) {
int cbr = (int) Math.round(Math.cbrt(x));
return cbr * cbr * cbr == x;
}
// Generate all subsequences and check for largest perfect cube
public static int largestCube(String n) {
int maxC = 0;
int len = n.length();
// Generate all subsequences
for (int i = 1; i < (1 << len); i++) {
StringBuilder sub = new StringBuilder();
for (int j = 0; j < len; j++) {
if ((i & (1 << j)) != 0) {
sub.append(n.charAt(j));
}
}
int num = Integer.parseInt(sub.toString());
if (isCube(num)) {
maxC = Math.max(maxC, num);
}
}
return maxC;
}
public static void main(String[] args) {
String n = "4125";
System.out.println(largestCube(n));
}
}
class GfG:
# Check if a number is a perfect cube
@staticmethod
def isCube(x):
cbr = round(x ** (1 / 3))
return cbr * cbr * cbr == x
# Generate all subsequences and check for largest perfect cube
@staticmethod
def largestCube(n):
maxC = 0
length = len(n)
# Generate all subsequences
for i in range(1, 1 << length):
sub = ""
for j in range(length):
if i & (1 << j):
sub += n[j]
num = int(sub)
if GfG.isCube(num):
maxC = max(maxC, num)
return maxC
if __name__ == "__main__":
n = "4125"
print(GfG.largestCube(n))
using System;
class GfG{
// Check if a number is a perfect cube
public static bool isCube(int x) {
int cbr = (int)Math.Round(Math.Cbrt(x));
return cbr * cbr * cbr == x;
}
// Generate all subsequences and check for largest perfect cube
public static int largestCube(string n) {
int maxC = 0;
int len = n.Length;
// Generate all subsequences
for (int i = 1; i < (1 << len); i++) {
string sub = "";
for (int j = 0; j < len; j++) {
if ((i & (1 << j)) != 0) {
sub += n[j];
}
}
int num = Int32.Parse(sub);
if (isCube(num)) {
maxC = Math.Max(maxC, num);
}
}
return maxC;
}
public static void Main(string[] args) {
string n = "4125";
Console.WriteLine(largestCube(n));
}
}
class GfG{
// Check if a number is a perfect cube
static isCube(x) {
let cbr = Math.round(Math.cbrt(x));
return cbr * cbr * cbr === x;
}
// Generate all subsequences and check for largest perfect cube
static largestCube(n) {
let maxC = 0;
let len = n.length;
// Generate all subsequences
for (let i = 1; i < (1 << len); i++) {
let sub = "";
for (let j = 0; j < len; j++) {
if (i & (1 << j)) {
sub += n[j];
}
}
let num = parseInt(sub);
if (GfG.isCube(num)) {
maxC = Math.max(maxC, num);
}
}
return maxC;
}
}
let n = "4125";
console.log(GfG.largestCube(n));
Output
125
Time Complexity: O(2log n log n), due to the number of digits in n are log(n) + 1
Auxiliary Space: O(log n), maximum size of subsequence will be log n
[Expected Approach] Checking cubes till n - O(n^1/3 log n) and O(log n) space
The expected approach is to iterate through potential perfect cubes up to a n, and for each perfect cube, check if it can be formed as a subsequence of the given number n. This avoids generating all subsequences explicitly.
#include <bits/stdc++.h>
using namespace std;
// Check if a number is a perfect cube
bool isCube(int x) {
int cbr = round(cbrt(x));
return cbr * cbr * cbr == x;
}
// Find the largest perfect cube that can be formed by deleting digits
int largestCube(string n) {
int maxC = 0, num=stoi(n);
// Iterate through possible cubes up to the limit
for (int i = 1; i * i * i < num; i++) {
int cube = i * i * i;
// Check if cube can be a subsequence of n
int idx = 0;
string cubeStr = to_string(cube);
for (char c : n) {
if (c == cubeStr[idx]) {
idx++;
}
if (idx == cubeStr.size()) {
maxC = max(maxC, cube);
break;
}
}
}
return maxC;
}
int main() {
string n = "4125";
cout << largestCube(n) << endl;
return 0;
}
import java.util.*;
public class GfG{
// Check if a number is a perfect cube
static boolean isCube(int x) {
int cbr = (int) Math.round(Math.cbrt(x));
return cbr * cbr * cbr == x;
}
// Find the largest perfect cube that can be formed by deleting digits
static int largestCube(String n) {
int maxC = 0;
int num = Integer.parseInt(n);
// Iterate through possible cubes up to the limit
for (int i = 1; i * i * i < num; i++) {
int cube = i * i * i;
// Check if cube can be a subsequence of n
int idx = 0;
String cubeStr = Integer.toString(cube);
for (char c : n.toCharArray()) {
if (c == cubeStr.charAt(idx)) {
idx++;
}
if (idx == cubeStr.length()) {
maxC = Math.max(maxC, cube);
break;
}
}
}
return maxC;
}
public static void main(String[] args) {
String n = "4125";
System.out.println(largestCube(n));
}
}
import math
# Check if a number is a perfect cube
def is_cube(x):
cbr = round(x ** (1/3))
return cbr * cbr * cbr == x
# Find the largest perfect cube that can be formed by deleting digits
def largest_cube(n):
max_c = 0
num = int(n)
# Iterate through possible cubes up to the limit
for i in range(1, int(num ** (1/3)) + 1):
cube = i * i * i
# Check if cube can be a subsequence of n
idx = 0
cube_str = str(cube)
for c in n:
if c == cube_str[idx]:
idx += 1
if idx == len(cube_str):
max_c = max(max_c, cube)
break
return max_c
if __name__ == '__main__':
n = "4125"
print(largest_cube(n))
using System;
class GfG{
// Check if a number is a perfect cube
static bool IsCube(int x) {
int cbr = (int)Math.Round(Math.Pow(x, 1.0/3.0));
return cbr * cbr * cbr == x;
}
// Find the largest perfect cube that can be formed by deleting digits
static int LargestCube(string n) {
int maxC = 0;
int num = int.Parse(n);
// Iterate through possible cubes up to the limit
for (int i = 1; i * i * i < num; i++) {
int cube = i * i * i;
// Check if cube can be a subsequence of n
int idx = 0;
string cubeStr = cube.ToString();
foreach (char c in n) {
if (c == cubeStr[idx]) {
idx++;
}
if (idx == cubeStr.Length) {
maxC = Math.Max(maxC, cube);
break;
}
}
}
return maxC;
}
static void Main() {
string n = "4125";
Console.WriteLine(LargestCube(n));
}
}
// Check if a number is a perfect cube
function isCube(x) {
let cbr = Math.round(Math.cbrt(x));
return cbr * cbr * cbr === x;
}
// Find the largest perfect cube that can be formed by deleting digits
function largestCube(n) {
let maxC = 0;
let num = parseInt(n);
// Iterate through possible cubes up to the limit
for (let i = 1; i * i * i < num; i++) {
let cube = i * i * i;
// Check if cube can be a subsequence of n
let idx = 0;
let cubeStr = cube.toString();
for (let c of n) {
if (c === cubeStr[idx]) {
idx++;
}
if (idx === cubeStr.length) {
maxC = Math.max(maxC, cube);
break;
}
}
}
return maxC;
}
let n = "4125";
console.log(largestCube(n));
Output
125
Time Complexity: O(n1/3 log(n)), due to the number of digits in n are log(n) + 1
Auxiliary Space: O(log n), maximum size of subsequence will be log n