Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
Last Updated :
15 Sep, 2023
Given N, count all ‘a’ and ‘b’ that satisfy the condition a^3 + b^3 = N.
Examples:
Input : N = 9
Output : 2
1^3 + 2^3 = 9
2^3 + 1^3 = 9
Input : N = 28
Output : 2
1^3 + 3^3 = 28
3^3 + 1^3 = 28
Note:- (a, b) and (b, a) are to be considered as two different pairs.
Asked in : Adobe
Implementation:
Traverse numbers from 1 to cube root of N.
a) Subtract cube of current number from
N and check if their difference is a
perfect cube or not.
i) If perfect cube then increment count.
2- Return count.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countPairs( int N)
{
int count = 0;
for ( int i = 1; i <= cbrt(N); i++)
{
int cb = i*i*i;
int diff = N - cb;
int cbrtDiff = cbrt(diff);
if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
count++;
}
return count;
}
int main()
{
for ( int i = 1; i<= 10; i++)
cout << "For n = " << i << ", "
<< countPairs(i) << " pair exists\n" ;
return 0;
}
|
Java
class Test
{
static int countPairs( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= Math.cbrt(N); i++)
{
int cb = i*i*i;
int diff = N - cb;
int cbrtDiff = ( int ) Math.cbrt(diff);
if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
count++;
}
return count;
}
public static void main(String args[])
{
for ( int i = 1 ; i<= 10 ; i++)
System.out.println( "For n = " + i + ", " +
+ countPairs(i) + " pair exists" );
}
}
|
Python 3
import math
def countPairs(N):
count = 0
for i in range ( 1 , int (math. pow (N, 1 / 3 ) + 1 )):
cb = i * i * i
diff = N - cb
cbrtDiff = int (math. pow (diff, 1 / 3 ))
if (cbrtDiff * cbrtDiff * cbrtDiff = = diff):
count + = 1
return count
for i in range ( 1 , 11 ):
print ( 'For n = ' , i, ', ' , countPairs(i),
' pair exists' )
|
C#
using System;
class Test
{
static int countPairs( int N)
{
int count = 0;
for ( int i = 1; i <= Math.Pow(N,(1.0/3.0)); i++)
{
int cb = i*i*i;
int diff = N - cb;
int cbrtDiff = ( int ) Math.Pow(diff,(1.0/3.0));
if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
count++;
}
return count;
}
public static void Main()
{
for ( int i = 1; i<= 10; i++)
Console.Write( "For n = " + i + ", " +
+ countPairs(i) + " pair exists" + "\n" );
}
}
|
PHP
<?php
function countPairs( $N )
{
$count = 0;
for ( $i = 1;
$i <= (int)pow( $N , 1 / 3); $i ++)
{
$cb = $i * $i * $i ;
$diff = ( $N - $cb );
$cbrtDiff = (int)pow( $diff , 1 / 3);
if ( $cbrtDiff * $cbrtDiff *
$cbrtDiff == $diff )
$count ++;
}
return $count ;
}
for ( $i = 1; $i <= 10; $i ++)
echo "For n = " , $i , ", " ,
countPairs( $i ) , " pair exists\n" ;
?>
|
Javascript
<script>
function countPairs(N)
{
let count = 0;
for (let i = 1; i <= parseInt(Math.pow(N,(1.0/3.0)), 10); i++)
{
let cb = i*i*i;
let diff = N - cb;
let cbrtDiff = parseInt(Math.pow(diff,(1.0/3.0)), 10);
if (cbrtDiff*cbrtDiff*cbrtDiff == diff)
count++;
}
return count;
}
for (let i = 1; i<= 10; i++)
document.write( "For n = " + i + ", " + countPairs(i) + " pair exists" + "</br>" );
</script>
|
Output
For n = 1, 1 pair exists
For n = 2, 1 pair exists
For n = 3, 0 pair exists
For n = 4, 0 pair exists
For n = 5, 0 pair exists
For n = 6, 0 pair exists
For n = 7, 0 pair exists
For n = 8, 1 pair exists
For n = 9, 2 pair exists
For n = 10, 0 pair exists
Time Complexity: O(N1/3) for each N.
Auxiliary Space: O(1)
Reference: https://www.careercup.com/question?id=5954491572551680
Share your thoughts in the comments
Please Login to comment...