Least number to be added to or subtracted from N to make it a Perfect Cube

Given a number
N
, Find the minimum number that needs to be added to or subtracted from
N
, to make it a
. 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 numberint 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 codeint main(){Â Â Â Â int n = 25;Â Â Â Â cout << nearest(n) << endl;Â
    n = 27;    cout << nearest(n) << endl;Â
    n = 40;    cout << nearest(n) << endl;Â
    return 0;} |
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 |
Python3
# Python3 implementation of the approachÂ
import mathÂ
# Function to return the least numberdef nearest(n):Â Â Â Â # Get the perfect cube before and after NÂ Â Â Â prev_cube = int(n ** (1/3))Â Â Â Â next_cube = prev_cube + 1Â Â Â Â prev_cube = prev_cube ** 3Â Â Â Â next_cube = next_cube ** 3Â
    # Check which is nearest to N    ans = prev_cube - n if n - prev_cube < next_cube - n else next_cube - nÂ
    # Return the result    return ansÂ
Â
# Driver codeif __name__ == "__main__":Â Â Â Â n = 25Â Â Â Â print(nearest(n))Â
    n = 27    print(nearest(n))Â
    n = 40    print(nearest(n))Â
Â
# by phasing17 |
C#
using System;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.Pow(n, 1.0 / 3.0));        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;        Console.WriteLine(Nearest(n));Â
        n = 27;        Console.WriteLine(Nearest(n));Â
        n = 40;        Console.WriteLine(Nearest(n));    }} |
Javascript
// Javascript implementation for the approachfunction nearest(n) {Â Â Â Â // Get the perfect cube before and after NÂ Â Â Â const prevCube = Math.floor(Math.cbrt(n));Â Â Â Â const nextCube = prevCube + 1;Â Â Â Â const prevCubeValue = prevCube * prevCube * prevCube;Â Â Â Â const nextCubeValue = nextCube * nextCube * nextCube;Â
    // Check which is nearest to N    const ans = Math.abs(n - prevCubeValue) < Math.abs(nextCubeValue - n)        ? (prevCubeValue - n)        : (nextCubeValue - n);Â
    // Return the result    return ans;}Â
// Driver codeconst n1 = 25;console.log(nearest(n1));Â
const n2 = 27;console.log(nearest(n2));Â
const n3 = 40;console.log(nearest(n3));Â
// This code is contributed by Taranpreet Singh. |
Output
2 0 -13
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



