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.
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.
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 :
(10, 27) (19, 24)
Time Complexity of above solution is O(n^(1/3)).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find Cube root of a number using Log function
- Centered cube number
- Count all triplets whose sum is equal to a perfect cube
- Program for cube sum of first n natural numbers
- C Program for cube sum of first n natural numbers
- C++ Program for cube sum of first n natural numbers
- Java Program for cube sum of first n natural numbers
- Python Program for cube sum of first n natural numbers
- Program for Volume and Surface Area of Cube
- N-th number which is both a square and a cube
- Cube Free Numbers smaller than n
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Count numbers upto N which are both perfect square and perfect cube
- Smallest perfect Cube divisible by all elements of an array
- Perfect Cube String
- Check whether N can be a Perfect Cube after adding or subtracting K