Check whether a number can be represented as difference of two consecutive cubes
Last Updated :
01 Oct, 2023
Given a number N, the task is to check if the number N can be represented as the difference of two consecutive cubes or not. If Yes then print those numbers else print No.
Examples:
Input: N = 19
Output:
Yes
2 3
Explanation:
33 – 23 = 19
Input: N = 10
Output: No
Approach: The key observation in the problem is that a number can be represented as difference of two consecutive cubes if and only if:
=> N = (K+1)3 – K3
=> N = 3*K2 + 3*K + 1
=> 12*N = 36*K2 + 36*K + 12
=> 12*N = (6*K + 3)2 + 3
=> 12*N – 3 = (6*K + 3)2
which means (12*N – 3) must be a perfect square to break N into difference of two consecutive cubes.
Therefore, if the above condition holds true then we will print the numbers using a for a loop by check that for which value of i if (i+1)3 – i3 = N and print the number i and i + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print( int N)
{
for ( int i = 0; i < 100000; i++) {
if ( pow (i + 1, 3)
- pow (i, 3)
== N) {
cout << i << ' ' << i + 1;
return ;
}
}
}
bool isPerfectSquare( long double x)
{
long double sr = sqrt (x);
return ((sr - floor (sr)) == 0);
}
bool diffCube( int N)
{
return isPerfectSquare(12 * N - 3);
}
int main()
{
int N = 19;
if (diffCube(N)) {
cout << "Yes\n" ;
print(N);
}
else {
cout << "No\n" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static void print( int N)
{
for ( int i = 0 ; i < 100000 ; i++)
{
if (Math.pow(i + 1 , 3 ) - Math.pow(i, 3 ) == N)
{
int j = i + 1 ;
System.out.println(i + " " + j);
return ;
}
}
}
static boolean isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0 );
}
static boolean diffCube( int N)
{
return isPerfectSquare( 12 * N - 3 );
}
public static void main(String[] args)
{
int N = 19 ;
if (diffCube(N))
{
System.out.println( "Yes" );
print(N);
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def printt(N):
for i in range ( 100000 ):
if ( pow (i + 1 , 3 ) - pow (i, 3 ) = = N):
print (i, '', i + 1 )
return
def isPerfectSquare(x):
sr = math.sqrt(x)
return ((sr - math.floor(sr)) = = 0 )
def diffCube(N):
return isPerfectSquare( 12 * N - 3 )
N = 19
if (diffCube(N)):
print ( "Yes" )
printt(N)
else :
print ( "No" )
|
C#
using System;
class GFG{
static void print( int N)
{
for ( int i = 0; i < 100000; i++)
{
if (Math.Pow(i + 1, 3) - Math.Pow(i, 3) == N)
{
int j = i + 1;
Console.WriteLine(i + " " + j);
return ;
}
}
}
static bool isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
return ((sr - Math.Floor(sr)) == 0);
}
static bool diffCube( int N)
{
return isPerfectSquare(12 * N - 3);
}
public static void Main(String[] args)
{
int N = 19;
if (diffCube(N))
{
Console.WriteLine( "Yes" );
print(N);
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function print(N)
{
for (let i = 0; i < 100000; i++)
{
if (parseInt(Math.pow(i + 1, 3), 10) -
parseInt(Math.pow(i, 3), 10) == N)
{
document.write(i + " " + (i + 1));
return ;
}
}
}
function isPerfectSquare(x)
{
let sr = Math.sqrt(x);
return ((sr - Math.floor(sr)) == 0);
}
function diffCube(N)
{
return isPerfectSquare(12 * N - 3);
}
let N = 19;
if (diffCube(N) != 0)
{
document.write( "Yes" + "</br>" );
print(N);
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N), where N is in the range 105
Auxiliary Space: O(1)
Approach 2: Binary Search:
The given program is used to determine whether a given number N can be expressed as the difference of two consecutive cubes.
- The approach used in the program is based on the fact that the difference of two consecutive cubes can be expressed as (j^3 – i^3), where j = i + 1.
- The program checks all possible values of i from 0 to 100000. For each value of i, it performs a binary search to find the value of j such that (j^3 – i^3) is equal to N. The search space for j is from i+1 to 100000 since j should always be greater than i. The binary search is performed by maintaining two pointers, lo and hi, that point to the left and right endpoints of the search space, respectively. The mid pointer is calculated as the average of lo and hi, and the difference (j^3 – i^3) is calculated for the mid value. If the difference is equal to N, the values of i and j are printed, and the function returns true. If the difference is less than N, the lo pointer is moved to mid+1 to search for a larger value of j. If the difference is greater than N, the hi pointer is moved to mid-1 to search for a smaller value of j. This process continues until either the pair (i, j) is found, or the search space is exhausted.
- If no such pair (i,j) is found for any value of i, the function returns false indicating that N cannot be expressed as the difference of two consecutive cubes.
- In the main function, a value of N=19 is given as input to the diffCube function. The function diffCube is called to determine if 19 can be expressed as the difference of two consecutive cubes. If such a pair exists, the program prints “Yes”. Otherwise, it prints “No”.
Here is the code of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool diffCube( int N)
{
for ( int i = 0; i <= 100000; i++) {
int lo = i + 1, hi = 100000, mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if ( pow (mid, 3) - pow (i, 3) == N) {
cout << i << " " << mid << endl;
return true ;
}
if ( pow (mid, 3) - pow (i, 3) < N)
lo = mid + 1;
else
hi = mid - 1;
}
}
return false ;
}
int main()
{
int N = 19;
if (diffCube(N))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
static boolean diffCube( int N) {
for ( int i = 0 ; i <= 100000 ; i++) {
int lo = i + 1 , hi = 100000 , mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2 ;
if (Math.pow(mid, 3 ) - Math.pow(i, 3 ) == N) {
System.out.println(i + " " + mid);
return true ;
}
if (Math.pow(mid, 3 ) - Math.pow(i, 3 ) < N)
lo = mid + 1 ;
else
hi = mid - 1 ;
}
}
return false ;
}
public static void main(String[] args) {
int N = 19 ;
if (diffCube(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
def diff_cube(N):
for i in range ( 0 , 100001 ):
lo, hi = i + 1 , 100000
while lo < = hi:
mid = lo + (hi - lo) / / 2
if (mid * * 3 - i * * 3 ) = = N:
print (i, mid)
return True
if (mid * * 3 - i * * 3 ) < N:
lo = mid + 1
else :
hi = mid - 1
return False
if __name__ = = '__main__' :
N = 19
if diff_cube(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class MainClass
{
public static bool DiffCube( int N)
{
for ( int i = 0; i <= 100000; i++)
{
int lo = i + 1, hi = 100000, mid;
while (lo <= hi)
{
mid = lo + (hi - lo) / 2;
if (Math.Pow(mid, 3) - Math.Pow(i, 3) == N)
{
Console.WriteLine($ "{i} {mid}" );
return true ;
}
if (Math.Pow(mid, 3) - Math.Pow(i, 3) < N)
lo = mid + 1;
else
hi = mid - 1;
}
}
return false ;
}
public static void Main( string [] args)
{
int N = 19;
if (DiffCube(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function diffCube(N) {
for (let i = 0; i <= 100000; i++) {
let lo = i + 1, hi = 100000, mid;
while (lo <= hi) {
mid = lo + Math.floor((hi - lo) / 2);
if (Math.pow(mid, 3) - Math.pow(i, 3) === N) {
console.log(i + " " + mid);
return true ;
}
if (Math.pow(mid, 3) - Math.pow(i, 3) < N)
lo = mid + 1;
else
hi = mid - 1;
}
}
return false ;
}
let N = 19;
if (diffCube(N))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N log^2(N)), where N is the given input number
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...