Find Cube Pairs | Set 1 (A n^(2/3) Solution)
Last Updated :
03 Jun, 2022
Given a number n, find two pairs that can represent the number as sum of two cubes. In other words, find two pairs (a, b) and (c, d) such that given number n can be expressed as
n = a^3 + b^3 = c^3 + d^3
where a, b, c and d are four distinct numbers.
Examples:
Input: N = 1729
Output: (1, 12) and (9, 10)
Explanation:
1729 = 1^3 + 12^3 = 9^3 + 10^3
Input: N = 4104
Output: (2, 16) and (9, 15)
Explanation:
4104 = 2^3 + 16^3 = 9^3 + 15^3
Input: N = 13832
Output: (2, 24) and (18, 20)
Explanation:
13832 = 2^3 + 24^3 = 18^3 + 20^3
Any number n that satisfies the constraint will have two distinct pairs (a, b) and (c, d) such that a, b, c and d are all less than n1/3. The idea is very simple. For every distinct pair (x, y) formed by numbers less than the n1/3, if their sum (x3 + y3) is equal to given number, we store them in a hash table using sum as a key. If pairs with sum equal to given number appears again, we simply print both pairs.
1) Create an empty hash map, say s.
2) cubeRoot = n1/3
3) for (int x = 1; x < cubeRoot; x++)
for (int y = x + 1; y <= cubeRoot; y++)
int sum = x3 + y3;
if (sum != n) continue;
if sum exists in s,
we found two pairs with sum, print the pairs
else
insert pair(x, y) in s using sum as key
Below is the implementation of above idea –
C++
#include <bits/stdc++.h>
using namespace std;
void findPairs( int n)
{
int cubeRoot = pow (n, 1.0/3.0);
unordered_map< int , pair< int , int > > s;
for ( int x = 1; x < cubeRoot; x++)
{
for ( int y = x + 1; y <= cubeRoot; y++)
{
int sum = x*x*x + y*y*y;
if (sum != n)
continue ;
if (s.find(sum) != s.end())
{
cout << "(" << s[sum].first << ", "
<< s[sum].second << ") and ("
<< x << ", " << y << ")" << endl;
}
else
s[sum] = make_pair(x, y);
}
}
}
int main()
{
int n = 13832;
findPairs(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPairs( int n)
{
int cubeRoot = ( int ) Math.pow(n, 1.0 / 3.0 );
HashMap<Integer, pair> s = new HashMap<Integer, pair>();
for ( int x = 1 ; x < cubeRoot; x++)
{
for ( int y = x + 1 ; y <= cubeRoot; y++)
{
int sum = x*x*x + y*y*y;
if (sum != n)
continue ;
if (s.containsKey(sum))
{
System.out.print( "(" + s.get(sum).first+ ", "
+ s.get(sum).second+ ") and ("
+ x+ ", " + y+ ")" + "\n" );
}
else
s.put(sum, new pair(x, y));
}
}
}
public static void main(String[] args)
{
int n = 13832 ;
findPairs(n);
}
}
|
Python3
def findPairs(n):
cubeRoot = pow (n, 1.0 / 3.0 );
s = {}
for x in range ( int (cubeRoot)):
for y in range (x + 1 ,
int (cubeRoot) + 1 ):
sum = x * x * x + y * y * y;
if ( sum ! = n):
continue ;
if sum in s.keys():
print ( "(" + str (s[ sum ][ 0 ]) +
", " + str (s[ sum ][ 1 ]) +
") and (" + str (x) +
", " + str (y) +
")" + "\n" )
else :
s[ sum ] = [x, y]
if __name__ = = "__main__" :
n = 13832
findPairs(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPairs( int n)
{
int cubeRoot = ( int ) Math.Pow(n, 1.0/3.0);
Dictionary< int , pair> s = new Dictionary< int , pair>();
for ( int x = 1; x < cubeRoot; x++)
{
for ( int y = x + 1; y <= cubeRoot; y++)
{
int sum = x*x*x + y*y*y;
if (sum != n)
continue ;
if (s.ContainsKey(sum))
{
Console.Write( "(" + s[sum].first+ ", "
+ s[sum].second+ ") and ("
+ x+ ", " + y+ ")" + "\n" );
}
else
s.Add(sum, new pair(x, y));
}
}
}
public static void Main(String[] args)
{
int n = 13832;
findPairs(n);
}
}
|
Javascript
function findPairs(n){
let cubeRoot = Math.floor(Math.pow(n, 1/3));
let s = new Map();
for (let x = 1; x < cubeRoot; x++){
for (let y = x + 1; y <= cubeRoot; y++){
let sum = x*x*x + y*y*y;
if (sum != n){
continue ;
}
if (s.has(sum)){
console.log( "(" , s.get(sum)[0], "," , s.get(sum)[1], ") and (" ,x, "," ,y, ")" );
}
else {
s.set(sum, [x, y]);
}
}
}
}
{
let n = 13832;
findPairs(n);
}
|
Output:
(2, 24) and (18, 20)
Time Complexity of above solution is O(n2/3) which is much less than O(n).
Can we solve the above problem in O(n1/3) time? We will be discussing that in next post.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...