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.
- Check whether (N + K) is a perfect cube or not
- If not, then check whether (N – K) is a perfect cube or not.
- If both are not perfect cube, then print “No”, else print “Yes”.
- 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++
#include <bits/stdc++.h>
using namespace std;
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" ;
}
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
class GFG {
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" );
}
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);
}
}
|
Python3
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" );
if __name__ = = "__main__" :
N = 7 ; K = 1 ;
canBePerfectCube(N, K);
N = 5 ; K = 4 ;
canBePerfectCube(N, K);
N = 7 ; K = 2 ;
canBePerfectCube(N, K);
|
C#
using System;
class GFG {
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" );
}
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);
}
}
|
Javascript
<script>
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>" );
}
var N = 7, K = 1;
canBePerfectCube(N, K);
N = 5, K = 4;
canBePerfectCube(N, K);
N = 7, K = 2;
canBePerfectCube(N, K);
</script>
|
Time Complexity : O(log(N+K)) because it is using cbrt function
Auxiliary Space: O(1)
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 geeks!