Given an integer N, the task is to check if this number can be represented as the sum of two consecutive perfect cubes or not.
Examples:
Input: N = 35
Output: Yes
Explanation:
Since, 35 = 23 + 33, therefore the required answer is Yes.
Input: N = 14
Output: No
Naive Approach: The simplest approach to solve the problem is to iterate from 1 to cube root of N and check if the sum of perfect cubes of any two consecutive numbers is equal to N or not. If found to be true, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isCubeSum( int n)
{
for ( int i = 1; i * i * i <= n; i++) {
if (i * i * i
+ (i + 1) * (i + 1) * (i + 1)
== n)
return true ;
}
return false ;
}
int main()
{
int n = 35;
if (isCubeSum(n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
static boolean isCubeSum( int n)
{
for ( int i = 1 ; i * i * i <= n; i++)
{
if (i * i * i + (i + 1 ) *
(i + 1 ) * (i + 1 ) == n)
return true ;
}
return false ;
}
public static void main(String[] args)
{
int n = 35 ;
if (isCubeSum(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isCubeSum(n):
for i in range ( 1 , int ( pow (n, 1 / 3 )) + 1 ):
if (i * i * i + (i + 1 ) *
(i + 1 ) * (i + 1 ) = = n):
return True ;
return False ;
if __name__ = = '__main__' :
n = 35 ;
if (isCubeSum(n)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool isCubeSum( int n)
{
for ( int i = 1; i * i * i <= n; i++)
{
if (i * i * i + (i + 1) *
(i + 1) * (i + 1) == n)
return true ;
}
return false ;
}
public static void Main(String[] args)
{
int n = 35;
if (isCubeSum(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isCubeSum(n)
{
for ( var i = 1; i * i * i <= n; i++) {
if (i * i * i
+ (i + 1) * (i + 1) * (i + 1)
== n)
return true ;
}
return false ;
}
var n = 35;
if (isCubeSum(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N1/3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- A number can be represented as the sum of the perfect cube of two consecutive numbers if the sum of the cube root of both consecutive numbers is equal to N.
- This can be checked by the formula:
![Rendered by QuickLaTeX.com \lfloor \sqrt[3]{N} - 1 \rfloor ^3 + \lfloor \sqrt[3]{N} \rfloor^3](https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-59e74925b276ff644cc733957260e587_l3.png)
- For example, if N = 35, then check if the equation below is equal to N or not:
![Rendered by QuickLaTeX.com \lfloor \sqrt[3]{35} - 1 \rfloor ^3 + \lfloor \sqrt[3]{35} \rfloor^3](https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-9c81706accdbadd9c5df0e455830d13a_l3.png)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSumCube( int N)
{
int a = cbrt(N);
int b = a - 1;
return ((a * a * a + b * b * b) == N);
}
int main()
{
int i = 35;
if (isSumCube(i)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean isSumCube( int N)
{
int a = ( int )Math.cbrt(N);
int b = a - 1 ;
return ((a * a * a + b * b * b) == N);
}
public static void main(String[] args)
{
int i = 35 ;
if (isSumCube(i))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def isSumCube(N):
a = int ( pow (N, 1 / 3 ))
b = a - 1
ans = ((a * a * a + b * b * b) = = N)
return ans
i = 35
if (isSumCube(i)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isSumCube( int N)
{
int a = ( int )Math.Pow(N, ( double ) 1 / 3);
int b = a - 1;
return ((a * a * a + b * b * b) == N);
}
public static void Main(String[] args)
{
int i = 35;
if (isSumCube(i))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isSumCube(N)
{
var a = parseInt(Math.cbrt(N));
var b = a - 1;
return ((a * a * a + b * b * b) == N);
}
var i = 35;
if (isSumCube(i))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(logN) because 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!
Last Updated :
26 Dec, 2022
Like Article
Save Article