Given a number **N**, at every step, subtract the largest perfect cube( ≤ N) from N. Repeat this step while **N > 0**. The task is to count the number of steps that can be performed.

**Examples:**

Input:N = 100

Output:4

First step, 100 – (4 * 4 * 4) = 100 – 64 = 36

Second step, 36 – (3 * 3 * 3) = 36 – 27 = 9

Third step, 9 – (2 * 2 * 2) = 9 – 8 = 1

Fourth step, 1 – (1 * 1 * 1) = 1 – 1 = 0

Input:N = 150

Output:5

First step, 150 – (5 * 5 * 5) = 150 – 125 = 25

Second step, 25 – (2 * 2 * 2) = 25 – 8 = 17

Third step, 17 – (2 * 2 * 2) = 17 – 8 = 9

Fourth step, 9 – (2 * 2 * 2) = 9 – 8 = 1

Fifth step, 1 – (1 * 1 * 1) = 1 – 1 = 0

**Approach**:

- Get the number from which the largest perfect cube has to be reduced.
- Find the cube root of the number and convert the result as an integer. The cube root of the number might contain some fraction part after the decimal, which needs to be avoided.
- Subtract the cube of the integer found in the previous step. This would remove the largest possible perfect cube from the number in the above step.
N = N - ((int) ∛N)

^{3} - Repeat the above two steps with the reduced number, till it is greater than 0.
- Print the number of times a perfect cube has been reduced from N. This is the final result.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of steps ` `int` `countSteps(` `int` `n) ` `{ ` ` ` ` ` `// Variable to store the count of steps ` ` ` `int` `steps = 0; ` ` ` ` ` `// Iterate while N > 0 ` ` ` `while` `(n) { ` ` ` ` ` `// Get the largest perfect cube ` ` ` `// and subtract it from N ` ` ` `int` `largest = cbrt(n); ` ` ` `n -= (largest * largest * largest); ` ` ` ` ` `// Increment steps ` ` ` `steps++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 150; ` ` ` `cout << countSteps(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG{ ` ` ` `// Function to return the count of steps ` `static` `int` `countSteps(` `int` `n) ` `{ ` ` ` ` ` `// Variable to store the count of steps ` ` ` `int` `steps = ` `0` `; ` ` ` ` ` `// Iterate while N > 0 ` ` ` `while` `(n > ` `0` `) { ` ` ` ` ` `// Get the largest perfect cube ` ` ` `// and subtract it from N ` ` ` `int` `largest = (` `int` `) Math.cbrt(n); ` ` ` `n -= (largest * largest * largest); ` ` ` ` ` `// Increment steps ` ` ` `steps++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `150` `; ` ` ` `System.out.print(countSteps(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `floor ` ` ` `# Function to return the count of steps ` `def` `countSteps(n): ` ` ` ` ` `# Variable to store the count of steps ` ` ` `steps ` `=` `0` ` ` ` ` `# Iterate while N > 0 ` ` ` `while` `(n): ` ` ` ` ` `# Get the largest perfect cube ` ` ` `# and subtract it from N ` ` ` `largest ` `=` `floor(n` `*` `*` `(` `1` `/` `3` `)) ` ` ` `n ` `-` `=` `(largest ` `*` `largest ` `*` `largest) ` ` ` ` ` `# Increment steps ` ` ` `steps ` `+` `=` `1` ` ` ` ` `# Return the required count ` ` ` `return` `steps ` ` ` `# Driver code ` `n ` `=` `150` `print` `(countSteps(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the count of steps ` `static` `int` `countSteps(` `int` `n) ` `{ ` ` ` ` ` `// Variable to store the count of steps ` ` ` `int` `steps = 0; ` ` ` ` ` `// Iterate while N > 0 ` ` ` `while` `(n > 0) { ` ` ` ` ` `// Get the largest perfect cube ` ` ` `// and subtract it from N ` ` ` `int` `largest = (` `int` `) Math.Pow(n,(` `double` `)1/3); ` ` ` `n -= (largest * largest * largest); ` ` ` ` ` `// Increment steps ` ` ` `steps++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 150; ` ` ` `Console.Write(countSteps(n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of times the largest perfect square number can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Least number to be added to or subtracted from N to make it a Perfect Square
- Count numbers upto N which are both perfect square and perfect cube
- Largest number in an array that is not a perfect cube
- Smallest number to be subtracted to convert given number to a palindrome
- Check whether N can be a Perfect Cube after adding or subtracting K
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Minimum divisor of a number to make the number perfect cube
- Previous perfect square and cube number smaller than number N
- Largest cube that can be inscribed within the sphere
- Largest sphere that can be inscribed inside a cube
- Largest cone that can be inscribed within a cube
- Largest cube that can be inscribed within a right circular cone
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Perfect cube greater than a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.