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 = 19Input: 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the two consecutive // numbers whose difference is N void print(int N) { // Iterate in the range [0, 10^5] for (int i = 0; i < 100000; i++) { if (pow(i + 1, 3) - pow(i, 3) == N) { cout << i << ' ' << i + 1; return; } } } // Function to check if N is a // perfect cube bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes bool diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code int main() { // Given Number N int N = 19; if (diffCube(N)) { cout << "Yes\n"; print(N); } else { cout << "No\n"; } return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to print the two consecutive // numbers whose difference is N static void print(int N) { // Iterate in the range [0, 10^5] 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; } } } // Function to check if N is a // perfect cube static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes static boolean diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code public static void main(String[] args) { // Given Number N int N = 19; if (diffCube(N)) { System.out.println("Yes"); print(N); } else { System.out.println("No"); } } } // This code is contributed by rock_cool |
Python3
# Python3 program for the above approach import math # Function to prthe two consecutive # numbers whose difference is N def printt(N): # Iterate in the range [0, 10^5] for i in range(100000): if (pow(i + 1, 3) - pow(i, 3) == N): print(i, '', i + 1) return # Function to check if N is a # perfect cube def isPerfectSquare(x): # Find floating povalue of # square root of x. sr = math.sqrt(x) # If square root is an integer return ((sr - math.floor(sr)) == 0) # Function to check whether a number # can be represented as difference # of two consecutive cubes def diffCube(N): # Check if 12 * N - 3 is a # perfect square or not return isPerfectSquare(12 * N - 3) # Driver Code # Given number N N = 19 if (diffCube(N)): print("Yes") printt(N) else: print("No") # This code is contributed by sanjoy_62 |
C#
// C# program for the above approach using System; class GFG{ // Function to print the two consecutive // numbers whose difference is N static void print(int N) { // Iterate in the range [0, 10^5] 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; } } } // Function to check if N is a // perfect cube static bool isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.Sqrt(x); // If square root is an integer return ((sr - Math.Floor(sr)) == 0); } // Function to check whether a number // can be represented as difference // of two consecutive cubes static bool diffCube(int N) { // Check if 12 * N - 3 is a // perfect square or not return isPerfectSquare(12 * N - 3); } // Driver Code public static void Main(String[] args) { // Given Number N int N = 19; if (diffCube(N)) { Console.WriteLine("Yes"); print(N); } else { Console.WriteLine("No"); } } } // This code is contributed by Rajput-Ji |
Yes 2 3
Time Complexity: O(N), where N is in the range 105
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a number can be represented as sum of two consecutive perfect cubes
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by sum of two squares
- Check whether a number can be represented by the product of two squares
- Check if N can be represented as sum of squares of two consecutive integers
- Check whether a number can be represented as sum of K distinct positive integers
- Count array elements that can be represented as sum of at least two consecutive array elements
- Check if given number can be represented as sum of two great numbers
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Count elements in an Array that can be represented as difference of two perfect squares
- Number of ways in which N can be represented as the sum of two positive integers
- Check if a given number can be represented in given a no. of digits in any base
- Check if a number can be represented as a sum of 2 triangular numbers
- Check if a number can be represented as sum of non zero powers of 2
- Check if N can be represented as sum of integers chosen from set {A, B}
- Number of perfect cubes between two given numbers
- Check whether a number has consecutive 0's in the given base or not
- Find the smallest positive number which can not be represented by given digits
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

