Check if a number can be represented as sum of two positive perfect cubes
Last Updated :
19 Apr, 2023
Given an integer N, the task is to check if N can be represented as the sum of two positive perfect cubes or not.
Examples:
Input: N = 28
Output: Yes
Explanation:
Since, 28 = 27 + 1 = 33 + 13.
Therefore, the required answer is Yes
Input: N = 34
Output: No
Approach: The idea is to store the perfect cubes of all numbers from 1 to cubic root of N in a Map and check if N can be represented as the sum of two numbers present in the Map or not. Follow the steps below to solve the problem:
- Initialize an ordered map, say cubes, to store the perfect cubes of first N natural numbers in sorted order.
- Traverse the map and check for the pair having a sum equal to N.
- If such a pair is found having sum N, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumOfTwoPerfectCubes( int N)
{
map< int , int > cubes;
for ( int i = 1; i * i * i <= N; i++)
cubes[i * i * i] = i;
map< int , int >::iterator itr;
for (itr = cubes.begin();
itr != cubes.end(); itr++) {
int firstNumber = itr->first;
int secondNumber = N - itr->first;
if (cubes.find(secondNumber)
!= cubes.end()) {
cout << "True" ;
return ;
}
}
cout << "False" ;
}
int main()
{
int N = 28;
sumOfTwoPerfectCubes(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void sumOfTwoPerfectCubes( int N)
{
HashMap<Integer, Integer> cubes = new HashMap<>();
for ( int i = 1 ; i * i * i <= N; i++)
cubes.put((i * i * i), i);
Iterator<Map.Entry<Integer, Integer> > itr
= cubes.entrySet().iterator();
while (itr.hasNext())
{
Map.Entry<Integer, Integer> entry = itr.next();
int firstNumber = entry.getKey();
int secondNumber = N - entry.getKey();
if (cubes.containsKey(secondNumber))
{
System.out.println( "True" );
return ;
}
}
System.out.println( "False" );
}
public static void main(String[] args)
{
int N = 28 ;
sumOfTwoPerfectCubes(N);
}
}
|
Python3
def sumOfTwoPerfectCubes(N) :
cubes = {}
i = 1
while i * i * i < = N :
cubes[i * i * i] = i
i + = 1
for itr in cubes :
firstNumber = itr
secondNumber = N - itr
if secondNumber in cubes :
print ( "True" , end = "")
return
print ( "False" , end = "")
N = 28
sumOfTwoPerfectCubes(N)
|
Javascript
<script>
function sumOfTwoPerfectCubes(N)
{
var cubes = new Map();
for ( var i = 1; i * i * i <= N; i++)
cubes.set(i * i * i, i);
var ans = false ;
cubes.forEach((value, key) => {
var firstNumber = key;
var secondNumber = N - value;
if (cubes.has(secondNumber)) {
document.write( "True" );
ans = true ;
return ;
}
});
if (ans)
{
return ;
}
document.write( "False" );
}
var N = 28;
sumOfTwoPerfectCubes(N);
</script>
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
public static void sumOfTwoPerfectCubes( int N)
{
Dictionary< int ,
int > cubes = new Dictionary< int ,
int >();
for ( int i = 1; i * i * i <= N; i++)
cubes.Add((i * i * i), i);
var val = cubes.Keys.ToList();
foreach ( var key in val)
{
int firstNumber = cubes[1];
int secondNumber = N - cubes[1];
if (cubes.ContainsKey(secondNumber))
{
Console.Write( "True" );
return ;
}
}
Console.Write( "False" );
}
static public void Main()
{
int N = 28;
sumOfTwoPerfectCubes(N);
}
}
|
Time Complexity: O(N1/3 * log(N1/3))
Auxiliary Space: O(N1/3)
Approach 2: Using two Pointers
We will declare lo to 1 and hi to cube root of n(the given number), then by (lo<=hi) this condition, if current is smaller than n we will increment the lo and in another hand if it is greater then decrement the hi, where current is (lo*lo*lo + hi*hi*hi)
C++
#include <bits/stdc++.h>
using namespace std;
bool sumOfTwoCubes( int n)
{
long long int lo = 1, hi = ( long long int )cbrt(n);
while (lo <= hi) {
long long int curr = (lo * lo * lo + hi * hi * hi);
if (curr == n)
return true ;
if (curr < n)
lo++;
else
hi--;
}
return false ;
}
int main()
{
int N = 28;
if (sumOfTwoCubes(N)) {
cout << "True" ;
}
else {
cout << "False" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean sumOfTwoCubes( int n)
{
int lo = 1 , hi = ( int )Math.cbrt(n);
while (lo <= hi) {
int curr = (lo * lo * lo + hi * hi * hi);
if (curr == n)
return true ;
if (curr < n)
lo++;
else
hi--;
}
return false ;
}
public static void main (String[] args)
{
int N = 28 ;
if (sumOfTwoCubes(N)) {
System.out.println( "True" );
}
else {
System.out.println( "False" );
}
}
}
|
Python3
import math
def sumOfTwoCubes(n):
lo = 1
hi = round (math. pow (n, 1 / 3 ))
while (lo < = hi):
curr = (lo * lo * lo + hi * hi * hi)
if (curr = = n):
return True
if (curr < n):
lo + = 1
else :
hi - = 1
return False
N = 28
if (sumOfTwoCubes(N)):
print ( "True" )
else :
print ( "False" )
|
Javascript
<script>
function sumOfTwoCubes(n)
{
var lo = 1, hi = (n);
while (lo <= hi) {
var curr = (lo * lo * lo + hi * hi * hi);
if (curr == n)
return true ;
if (curr < n)
lo++;
else
hi--;
}
return false ;
}
var N = 28;
if (sumOfTwoCubes(N)) {
document.write( "True" );
}
else {
document.write( "False" );
}
</script>
|
C#
using System;
class GFG
{
static bool sumOfTwoCubes( int n)
{
int lo = 1, hi = ( int )Math.Pow(n, (1.0 / 3.0));
while (lo <= hi) {
int curr = (lo * lo * lo + hi * hi * hi);
if (curr == n)
return true ;
if (curr < n)
lo++;
else
hi--;
}
return false ;
}
public static void Main (String[] args)
{
int N = 28;
if (sumOfTwoCubes(N)) {
Console.Write( "True" );
}
else {
Console.Write( "False" );
}
}
}
|
Time Complexity : O(log(cbrt(n))),where n is given number
Auxiliary Space: O(1)
Approach 3: Dynamic Programming
Here’s the Dynamic Programming (DP) approach to solve the problem of finding whether a given number N can be represented as the sum of two perfect cubes or not:
We can create a DP array dp[N+1], where dp[i] is set to 1 if the number i can be represented as the sum of two perfect cubes, and 0 otherwise. We can initialize dp[0] as 1 (since 0 can be represented as the sum of two 0’s), and dp[i] as 0 for all i > 0.
Then, we can iterate over all possible pairs of perfect cubes (i^3 + j^3) such that i^3 + j^3 <= N, and mark dp[i^3 + j^3] as 1. This can be done using two nested loops:
Here is the code below:
C++
#include <bits/stdc++.h>
using namespace std;
bool sumOfTwoPerfectCubes( int N)
{
vector< int > dp(N+1, 0);
dp[0] = 1;
for ( int i = 1; i * i * i <= N; i++) {
for ( int j = 1; j * j * j <= N - i * i * i; j++) {
int sum = i * i * i + j * j * j;
if (sum <= N) dp[sum] = 1;
}
}
return dp[N];
}
int main()
{
int N = 28;
if (sumOfTwoPerfectCubes(N)) {
cout << "True" ;
} else {
cout << "False" ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean sumOfTwoPerfectCubes( int N)
{
int [] dp = new int [N + 1 ];
dp[ 0 ] = 1 ;
for ( int i = 1 ; i * i * i <= N; i++) {
for ( int j = 1 ; j * j * j <= N - i * i * i;
j++) {
int sum = i * i * i + j * j * j;
if (sum <= N)
dp[sum] = 1 ;
}
}
return dp[N] == 1 ;
}
public static void main(String[] args)
{
int N = 28 ;
if (sumOfTwoPerfectCubes(N)) {
System.out.println( "True" );
}
else {
System.out.println( "False" );
}
}
}
|
Python3
def sumOfTwoPerfectCubes(N):
dp = [ 0 ] * (N + 1 )
dp[ 0 ] = 1
for i in range ( 1 , int (N * * ( 1 / 3 )) + 1 ):
for j in range ( 1 , int ((N - i * * 3 ) * * ( 1 / 3 )) + 1 ):
sum = i * * 3 + j * * 3
if sum < = N:
dp[ sum ] = 1
return dp[N]
N = 28
if sumOfTwoPerfectCubes(N):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
public class Program {
public static bool SumOfTwoPerfectCubes( int N)
{
int [] dp = new int [N + 1];
dp[0] = 1;
for ( int i = 1; i * i * i <= N; i++) {
for ( int j = 1; j * j * j <= N - i * i * i;
j++) {
int sum = i * i * i + j * j * j;
if (sum <= N)
dp[sum] = 1;
}
}
return dp[N] == 1;
}
public static void Main()
{
int N = 28;
if (SumOfTwoPerfectCubes(N)) {
Console.WriteLine( "True" );
}
else {
Console.WriteLine( "False" );
}
}
}
|
Javascript
function sumOfTwoPerfectCubes(N) {
const dp = new Array(N + 1).fill(0);
dp[0] = 1;
for (let i = 1; i <= Math.floor(Math.pow(N, 1/3)); i++) {
for (let j = 1; j <= Math.floor(Math.pow(N - Math.pow(i, 3), 1/3)); j++) {
const sum = Math.pow(i, 3) + Math.pow(j, 3);
if (sum <= N) {
dp[sum] = 1;
}
}
}
return dp[N] === 1;
}
const N = 28;
if (sumOfTwoPerfectCubes(N)) {
console.log( "True" );
} else {
console.log( "False" );
}
|
Output
True
Time Complexity: O(N^(2/3)), where N is the input number.
Auxiliary Space: O(N), as we are using a DP array of size N+1 to store
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...