Check whether N can be a Perfect Cube after adding or subtracting K

Given two integers N and K, the task is to check whether N can be made a perfect cube after adding to or subtracting from K to or from it.

Examples:

Input: N = 7, K = 1
Output: Yes
7 + 1 = 8 which is a perfect cube (23 = 8)

Input: N = 5, K = 4
Output: Yes
5 – 4 = 1 which is a perfect cube (13 = 1)

Approach: The simplest way to solve this problem is to check whether either (N + K) or (N – K) is a perfect cube or not.



  1. Check whether (N + K) is a perfect cube or not
  2. If not, then check whether (N – K) is a perfect cube or not.
  3. If both are not perfect cube, then print “No”, else print “Yes”.
  4. In order to check whether a number is a perfect cube or not, the easiest way is to find the cube of the floor value of cube root of the number, and then check whether this cube is same as the number or not.
    if(N3 == (floor(∛N))3)
    Then N is a perfect cube
    

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if a number is
// a perfect Cube or not
bool isPerfectCube(int x)
{
    int cr = round(cbrt(x));
    return (cr * cr * cr == x);
}
  
void canBePerfectCube(int N, int K)
{
    if (isPerfectCube(N + K)
        || isPerfectCube(N - K))
        cout << "Yes\n";
    else
        cout << "No\n";
}
  
// Driver code
int main()
{
    int N = 7, K = 1;
    canBePerfectCube(N, K);
  
    N = 5, K = 4;
    canBePerfectCube(N, K);
  
    N = 7, K = 2;
    canBePerfectCube(N, K);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
class GFG {
  
    // Function to check if a number is
    // a perfect Cube or not
    static boolean isPerfectCube(int x)
    {
        int cr = (int)Math.cbrt(x);
        return (cr * cr * cr == x);
    }
  
    static void canBePerfectCube(int N, int K)
    {
        if (isPerfectCube(N + K)
            || isPerfectCube(N - K) == true)
            System.out.println("Yes");
        else
             System.out.println("No");
    }
  
    // Driver code
    public static void main (String[] args)
    {
        int N = 7;
        int K = 1;
        canBePerfectCube(N, K);
      
        N = 5
        K = 4;
        canBePerfectCube(N, K);
      
        N = 7; K = 2;
        canBePerfectCube(N, K);
      
    }
}
  
// This code is contributed by Yash_R

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to check if a number is
# a perfect Cube or not
def isPerfectCube(x) :
    cr = int(x ** (1/3));
    return (cr * cr * cr == x);
  
def canBePerfectCube(N, K) :
    if (isPerfectCube(N + K) or isPerfectCube(N - K)) :
        print("Yes");
    else :
        print("No");
  
# Driver code
if __name__ == "__main__"
  
    N = 7; K = 1;
    canBePerfectCube(N, K);
  
    N = 5; K = 4;
    canBePerfectCube(N, K);
  
    N = 7; K = 2;
    canBePerfectCube(N, K);
  
# This code is contributed by Yash_R

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG {
  
    // Function to check if a number is
    // a perfect Cube or not
    static bool isPerfectCube(int x)
    {
        int cr = (int)Math.Cbrt(x);
        return (cr * cr * cr == x);
    }
  
    static void canBePerfectCube(int N, int K)
    {
        if (isPerfectCube(N + K)
            || isPerfectCube(N - K) == true)
            Console.WriteLine("Yes");
        else
             Console.WriteLine("No");
    }
  
    // Driver code
    public static void Main (string[] args)
    {
        int N = 7;
        int K = 1;
        canBePerfectCube(N, K);
      
        N = 5; 
        K = 4;
        canBePerfectCube(N, K);
      
        N = 7; K = 2;
        canBePerfectCube(N, K);
      
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

Yes
Yes
No

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Yash_R, AnkitRai01

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.