using System;
using System.Collections.Generic;
class GFG
{
static List<String> preProcess(int n)
{
List<String> preProcessedCubes = new List<String>();
for (int i = 1; i * i * i <= n; i++)
{
int iThCube = i * i * i;
String cubeString = String.Join("",iThCube);
preProcessedCubes.Add(cubeString);
}
return preProcessedCubes;
}
static String findLargestCubeUtil(String num,
List<String> preProcessedCubes)
{
preProcessedCubes.Reverse();
int totalCubes = preProcessedCubes.Count;
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";
}
static void findLargestCube(int n)
{
List<String> preProcessedCubes = preProcess(n);
String num = String.Join("",n);
String ans = findLargestCubeUtil(num, preProcessedCubes);
Console.WriteLine("Largest Cube that can be formed from "
+ n + " is " + ans);
}
public static void Main(String[] args)
{
int n;
n = 4125;
findLargestCube(n);
n = 876;
findLargestCube(n);
}
}