Related Articles
Check whether N can be a Perfect Cube after adding or subtracting K
• Last Updated : 09 Apr, 2021

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```
1.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach` `#include ``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

 ``
Output:
```Yes
Yes
No```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up