Given an integer **N**, the task is to check if this number can be represented as the sum of two consecutive perfect cubes or not.

**Examples:**

Input:N = 35Output:YesExplanation:

Since, 35 = 2^{3}+ 3^{3}, therefore the required answer is Yes.

Input:N = 14Output:No

**Naive Approach:** The simplest approach to solve the problem is to iterate from 1 to cube root of **N** and check if the sum of perfect cubes of any two consecutive numbers is equal to** N** or not. If found to be true, print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

## C++

`// C++ Program of the` `// above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if a number` `// can be expressed as the sum of` `// cubes of two consecutive numbers` `bool` `isCubeSum(` `int` `n)` `{` ` ` `for` `(` `int` `i = 1; i * i * i <= n; i++) {` ` ` `if` `(i * i * i` ` ` `+ (i + 1) * (i + 1) * (i + 1)` ` ` `== n)` ` ` `return` `true` `;` ` ` `}` ` ` `return` `false` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 35;` ` ` `if` `(isCubeSum(n))` ` ` `cout << ` `"Yes"` `;` ` ` `else` ` ` `cout << ` `"No"` `;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program of the` `// above approach` `import` `java.util.*;` `class` `GFG{` `// Function to check if a number` `// can be expressed as the sum of` `// cubes of two consecutive numbers` `static` `boolean` `isCubeSum(` `int` `n)` `{` ` ` `for` `(` `int` `i = ` `1` `; i * i * i <= n; i++)` ` ` `{` ` ` `if` `(i * i * i + (i + ` `1` `) *` ` ` `(i + ` `1` `) * (i + ` `1` `) == n)` ` ` `return` `true` `;` ` ` `}` ` ` `return` `false` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `n = ` `35` `;` ` ` `if` `(isCubeSum(n))` ` ` `System.out.print(` `"Yes"` `);` ` ` `else` ` ` `System.out.print(` `"No"` `);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program of the` `# above approach` `# Function to check if a number` `# can be expressed as the sum of` `# cubes of two consecutive numbers` `def` `isCubeSum(n):` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `int` `(` `pow` `(n, ` `1` `/` `3` `)) ` `+` `1` `):` ` ` `if` `(i ` `*` `i ` `*` `i ` `+` `(i ` `+` `1` `) ` `*` ` ` `(i ` `+` `1` `) ` `*` `(i ` `+` `1` `) ` `=` `=` `n):` ` ` `return` `True` `;` ` ` `return` `False` `;` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `n ` `=` `35` `;` ` ` `if` `(isCubeSum(n)):` ` ` `print` `(` `"Yes"` `);` ` ` `else` `:` ` ` `print` `(` `"No"` `);` `# This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## C#

`// C# program of the` `// above approach` `using` `System;` `class` `GFG{` `// Function to check if a number` `// can be expressed as the sum of` `// cubes of two consecutive numbers` `static` `bool` `isCubeSum(` `int` `n)` `{` ` ` `for` `(` `int` `i = 1; i * i * i <= n; i++)` ` ` `{` ` ` `if` `(i * i * i + (i + 1) *` ` ` `(i + 1) * (i + 1) == n)` ` ` `return` `true` `;` ` ` `}` ` ` `return` `false` `;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `n = 35;` ` ` `if` `(isCubeSum(n))` ` ` `Console.Write(` `"Yes"` `);` ` ` `else` ` ` `Console.Write(` `"No"` `);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Efficient Approach:** The above approach can be optimized based on the following observations:

- A number can be represented as the sum of the perfect cube of two consecutive numbers if the sum of the cube root of both consecutive numbers is equal to
**N**. - This can be checked by the formula:

- For example, if
**N = 35**, then check of the equation below os equal to**N**or not:

Below is the implementation of the above approach:

## C++

`// C++ Program to` `// implement above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check that a number` `// is the sum of cubes of 2` `// consecutive numbers or not` `bool` `isSumCube(` `int` `N)` `{` ` ` `int` `a = cbrt(N);` ` ` `int` `b = a - 1;` ` ` `// Condition to check if a` ` ` `// number is the sum of cubes of 2` ` ` `// consecutive numbers or not` ` ` `return` `((a * a * a + b * b * b) == N);` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `i = 35;` ` ` `// Function call` ` ` `if` `(isSumCube(i)) {` ` ` `cout << ` `"Yes"` `;` ` ` `}` ` ` `else` `{` ` ` `cout << ` `"No"` `;` ` ` `}` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// above approach` `class` `GFG{` `// Function to check that a number` `// is the sum of cubes of 2` `// consecutive numbers or not` `static` `boolean` `isSumCube(` `int` `N)` `{` ` ` `int` `a = (` `int` `)Math.cbrt(N);` ` ` `int` `b = a - ` `1` `;` ` ` `// Condition to check if a` ` ` `// number is the sum of cubes of 2` ` ` `// consecutive numbers or not` ` ` `return` `((a * a * a + b * b * b) == N);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `i = ` `35` `;` ` ` ` ` `// Function call` ` ` `if` `(isSumCube(i)) ` ` ` `{` ` ` `System.out.print(` `"Yes"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `System.out.print(` `"No"` `);` ` ` `}` `}` `}` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to ` `# implement above approach ` `# Function to check that a number ` `# is the sum of cubes of 2` `# consecutive numbers or not` `def` `isSumCube(N):` ` ` `a ` `=` `int` `(` `pow` `(N, ` `1` `/` `3` `))` ` ` `b ` `=` `a ` `-` `1` ` ` `# Condition to check if a` ` ` `# number is the sum of cubes of 2` ` ` `# consecutive numbers or not` ` ` `ans ` `=` `((a ` `*` `a ` `*` `a ` `+` `b ` `*` `b ` `*` `b) ` `=` `=` `N)` ` ` `return` `ans` `# Driver Code` `i ` `=` `35` `# Function call` `if` `(isSumCube(i)):` ` ` `print` `(` `"Yes"` `)` `else` `:` ` ` `print` `(` `"No"` `)` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// above approach` `using` `System;` `class` `GFG{` `// Function to check that a number` `// is the sum of cubes of 2` `// consecutive numbers or not` `static` `bool` `isSumCube(` `int` `N)` `{` ` ` `int` `a = (` `int` `)Math.Pow(N, (` `double` `) 1 / 3);` ` ` `int` `b = a - 1;` ` ` `// Condition to check if a` ` ` `// number is the sum of cubes of 2` ` ` `// consecutive numbers or not` ` ` `return` `((a * a * a + b * b * b) == N);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `i = 35;` ` ` ` ` `// Function call` ` ` `if` `(isSumCube(i)) ` ` ` `{` ` ` `Console.Write(` `"Yes"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `Console.Write(` `"No"` `);` ` ` `}` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity: **O(1) **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 whether a number can be represented as difference of two consecutive cubes
- Check if N can be represented as sum of squares of two consecutive integers
- Count array elements that can be represented as sum of at least two consecutive array elements
- Count elements in an Array that can be represented as difference of two perfect squares
- Number of perfect cubes between two given numbers
- Check if given number can be represented as sum of two great numbers
- Check whether a number can be represented by sum of two squares
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check whether a number can be represented as difference of two squares
- Check whether a number can be represented by the product of two squares
- Number of ways in which N can be represented as the sum of two positive integers
- Check if a number can be expressed as sum of two Perfect powers
- 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 whether a number can be represented as sum of K distinct positive integers
- Perfect cubes in a range
- Smallest and Largest N-digit perfect cubes
- Check if a number is a perfect square having all its digits as a perfect square

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.