Open In App

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

Last Updated : 12 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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


Python3




# 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


C#




// 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


Javascript




<script>
 
 
// Javascript implementation of the above approach
 
// Function to check if a number is
// a perfect Cube or not
function isPerfectCube(x)
{
    var cr = Math.round(Math.cbrt(x));
    return (cr * cr * cr == x);
}
 
function canBePerfectCube(N, K)
{
    if (isPerfectCube(N + K)
        || isPerfectCube(N - K))
        document.write("Yes<br>");
    else
        document.write("No<br>");
}
 
// Driver code
var 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 rrrtnx.
</script>


Output: 

Yes
Yes
No

 

Time Complexity : O(log(N+K)) because it is using cbrt function
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads