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.
Input: N = 25
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
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
- 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:
2 0 -13
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Minimum divisor of a number to make the number perfect cube
- Number of times the largest perfect square number can be subtracted from N
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Previous perfect square and cube number smaller than number N
- Check if number formed by joining two Numbers is Perfect Cube
- Count numbers upto N which are both perfect square and perfect cube
- Find the minimum number to be added to N to make it a prime number
- Minimum number to be added to all digits of X to make X > Y
- Find the minimum number to be added to N to make it a power of K
- Minimum number of Parentheses to be added to make it valid
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Find minimum number to be divided to make a number a perfect square
- Minimum digits to remove to make a number Perfect Square
- Perfect Cube
- Perfect Cube String
- Count of subarrays having sum as a perfect cube
- Smallest perfect cube in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Yash_R