Find Cube Pairs | Set 2 (A n^(1/3) Solution)
Last Updated :
03 Mar, 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
We have discussed a O(n2/3) solution in below set 1.
Find Cube Pairs | Set 1 (A n^(2/3) Solution)
In this post, a O(n1/3) solution is discussed.
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 to create an auxiliary array of size n1/3. Each index i in the array will store value equal to cube of that index i.e. arr[i] = i^3. Now the problem reduces to finding pair of elements in an sorted array whose sum is equal to given number n. The problem is discussed in detail here.
Below is the implementation of above idea :
C++
#include <iostream>
#include <cmath>
using namespace std;
void findPairs( int n)
{
int cubeRoot = pow (n, 1.0 / 3.0);
int cube[cubeRoot + 1];
for ( int i = 1; i <= cubeRoot; i++)
cube[i] = i*i*i;
int l = 1;
int r = cubeRoot;
while (l < r)
{
if (cube[l] + cube[r] < n)
l++;
else if (cube[l] + cube[r] > n)
r--;
else {
cout << "(" << l << ", " << r
<< ")" << endl;
l++; r--;
}
}
}
int main()
{
int n = 20683;
findPairs(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findPairs( int n)
{
int cubeRoot = ( int )Math.pow(
n, 1.0 / 3.0 );
int cube[] = new int [cubeRoot + 1 ];
for ( int i = 1 ; i <= cubeRoot; i++)
cube[i] = i * i * i;
int l = 1 ;
int r = cubeRoot;
while (l < r)
{
if (cube[l] + cube[r] < n)
l++;
else if (cube[l] + cube[r] > n)
r--;
else {
System.out.println( "(" + l +
", " + r +
")" );
l++; r--;
}
}
}
public static void main (String[] args)
{
int n = 20683 ;
findPairs(n);
}
}
|
Python3
import math
def findPairs( n):
cubeRoot = int (math. pow (n, 1.0 / 3.0 ));
cube = [ 0 ] * (cubeRoot + 1 );
for i in range ( 1 , cubeRoot + 1 ):
cube[i] = i * i * i;
l = 1 ;
r = cubeRoot;
while (l < r):
if (cube[l] + cube[r] < n):
l + = 1 ;
else if (cube[l] + cube[r] > n):
r - = 1 ;
else :
print ( "(" , l , ", " , math.floor(r),
")" , end = "");
print ();
l + = 1 ;
r - = 1 ;
n = 20683 ;
findPairs(n);
|
C#
using System;
class GFG
{
static void findPairs( int n)
{
int cubeRoot = ( int )Math.Pow(n, 1.0 /
3.0);
int []cube = new int [cubeRoot + 1];
for ( int i = 1; i <= cubeRoot; i++)
cube[i] = i * i * i;
int l = 1;
int r = cubeRoot;
while (l < r)
{
if (cube[l] + cube[r] < n)
l++;
else if (cube[l] + cube[r] > n)
r--;
else {
Console.WriteLine( "(" + l +
", " + r +
")" );
l++; r--;
}
}
}
public static void Main ()
{
int n = 20683;
findPairs(n);
}
}
|
PHP
<?php
function findPairs( $n )
{
$cubeRoot = pow( $n , 1.0 / 3.0);
$cube = array ();
for ( $i = 1; $i <= $cubeRoot ; $i ++)
$cube [ $i ] = $i * $i * $i ;
$l = 1;
$r = $cubeRoot ;
while ( $l < $r )
{
if ( $cube [ $l ] + $cube [ $r ] < $n )
$l ++;
else if ( $cube [ $l ] + $cube [ $r ] > $n )
$r --;
else
{
echo "(" , $l , ", " , floor ( $r )
, ")" ;
echo "\n" ;
$l ++; $r --;
}
}
}
$n = 20683;
findPairs( $n );
?>
|
Javascript
<script>
function findPairs(n)
{
var cubeRoot = parseInt(Math.pow(
n, 1.0 / 3.0));
var cube = Array.from({length: cubeRoot + 1}, (_, i) => 0);
for (i = 1; i <= cubeRoot; i++)
cube[i] = i * i * i;
var l = 1;
var r = cubeRoot;
while (l < r)
{
if (cube[l] + cube[r] < n)
l++;
else if (cube[l] + cube[r] > n)
r--;
else {
document.write( "(" + l +
", " + r +
")<br>" );
l++; r--;
}
}
}
var n = 20683;
findPairs(n);
</script>
|
Output:
(10, 27)
(19, 24)
Time Complexity of above solution is O(n^(1/3)).
Share your thoughts in the comments
Please Login to comment...