Given a number **N**, Find the minimum number that needs to be added to or subtracted from **N**, to make it a perfect cube. If the number is to be added, print it with a + sign, else if the number is to be subtracted, print it with a – sign.

**Examples:**

Input:N = 25

Output:2

Nearest perfect cube before 25 = 8

Nearest perfect cube after 25 = 27

Therefore 2 needs to be added to 25 to get the closest perfect cube

Input:N = 40

Output:-13

Nearest perfect cube before 40 = 25

Nearest perfect cube after 40 = 64

Therefore 13 needs to be subtracted from 40 to get the closest perfect cube

**Approach**:

- Get the number.
- Find the cube root of the number and convert the result as an integer.
- After converting the double value to integer, this will contain the root of the perfect cube before N, i.e.
**floor(cube root(N))**. - Then find the cube of this number, which will be the perfect cube before N.
- Find the root of the perfect cube after N, i.e. the
**ceil(cube root(N))**. - Then find the cube of this number, which will be the perfect cube after N.
- Check whether the cube of floor value is nearest to N or the ceil value.
- If the cube of floor value is nearest to N, print the difference with a -sign. Else print the difference between the cube of the ceil value and N with a + sign.

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 Least number ` `int` `nearest(` `int` `n) ` `{ ` ` ` ` ` `// Get the perfect cube ` ` ` `// before and after N ` ` ` `int` `prevCube = cbrt(n); ` ` ` `int` `nextCube = prevCube + 1; ` ` ` `prevCube = prevCube * prevCube * prevCube; ` ` ` `nextCube = nextCube * nextCube * nextCube; ` ` ` ` ` `// Check which is nearest to N ` ` ` `int` `ans ` ` ` `= (n - prevCube) < (nextCube - n) ` ` ` `? (prevCube - n) ` ` ` `: (nextCube - n); ` ` ` ` ` `// return the result ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 25; ` ` ` `cout << nearest(n) << endl; ` ` ` ` ` `n = 27; ` ` ` `cout << nearest(n) << endl; ` ` ` ` ` `n = 40; ` ` ` `cout << nearest(n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the Least number ` ` ` `static` `int` `nearest(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Get the perfect cube ` ` ` `// before and after N ` ` ` `int` `prevCube = (` `int` `)Math.cbrt(n); ` ` ` `int` `nextCube = prevCube + ` `1` `; ` ` ` `prevCube = prevCube * prevCube * prevCube; ` ` ` `nextCube = nextCube * nextCube * nextCube; ` ` ` ` ` `// Check which is nearest to N ` ` ` `int` `ans = (n - prevCube) < (nextCube - n) ? ` ` ` `(prevCube - n) : (nextCube - n); ` ` ` ` ` `// return the result ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `25` `; ` ` ` `System.out.println(nearest(n)); ` ` ` ` ` `n = ` `27` `; ` ` ` `System.out.println(nearest(n)) ; ` ` ` ` ` `n = ` `40` `; ` ` ` `System.out.println(nearest(n)) ; ` ` ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

2 0 -13

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:

- Least number to be added to or subtracted from N to make it a Perfect Square
- Number of times the largest Perfect Cube can be subtracted from N
- Number of times the largest perfect square number can be subtracted from N
- Count numbers upto N which are both perfect square and perfect cube
- Minimum divisor of a number to make the number perfect cube
- Smallest number to be subtracted to convert given number to a palindrome
- 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
- Previous perfect square and cube number smaller than number N
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Check if number formed by joining two Numbers is Perfect Cube
- Perfect Cube factors of a Number
- C Program to check whether a number is a Perfect Cube or not
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Print N numbers such that their sum is a Perfect Cube
- Perfect Cube String
- Check whether N can be a Perfect Cube after adding or subtracting K
- Count of pairs in an Array whose sum is a Perfect Cube

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.