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.
Examples:Ā
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
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 very simple. For every distinct pair (x, y) formed by numbers less than the n1/3, if their sum (x3 + y3) is equal to given number, we store them in a hash table using sum as a key. If pairs with sum equal to given number appears again, we simply print both pairs.
1) Create an empty hash map, say s. 2) cubeRoot = n1/3 3) for (int x = 1; x < cubeRoot; x++) for (int y = x + 1; y <= cubeRoot; y++) int sum = x3 + y3; if (sum != n) continue; if sum exists in s, we found two pairs with sum, print the pairs else insert pair(x, y) in s using sum as key
Below is the implementation of above idea āĀ
C++
// C++ program to find pairs that can represent // the given number as sum of two cubes #include <bits/stdc++.h> using namespace std; Ā
// Function to find pairs that can represent // the given number as sum of two cubes void findPairs( int n) { Ā Ā Ā Ā // find cube root of n Ā Ā Ā Ā int cubeRoot = pow (n, 1.0/3.0); Ā
Ā Ā Ā Ā // create an empty map Ā Ā Ā Ā unordered_map< int , pair< int , int > > s; Ā
Ā Ā Ā Ā // Consider all pairs such with values less Ā Ā Ā Ā // than cuberoot Ā Ā Ā Ā for ( int x = 1; x < cubeRoot; x++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int y = x + 1; y <= cubeRoot; y++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find sum of current pair (x, y) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int sum = x*x*x + y*y*y; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // do nothing if sum is not equal to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // given number Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (sum != n) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen before, we found two pairs Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s.find(sum) != s.end()) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "(" << s[sum].first << ", " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << s[sum].second << ") and (" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << x << ", " << y << ")" << endl; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen for the first time Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s[sum] = make_pair(x, y); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver function int main() { Ā Ā Ā Ā int n = 13832; Ā Ā Ā Ā findPairs(n); Ā Ā Ā Ā return 0; } |
Java
// Java program to find pairs that can represent // the given number as sum of two cubes import java.util.*; Ā
class GFG { Ā Ā Ā Ā static class pair Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int first, second; Ā Ā Ā Ā Ā Ā Ā Ā public pair( int first, int second) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this .first = first; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this .second = second; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā Ā // Function to find pairs that can represent // the given number as sum of two cubes static void findPairs( int n) { Ā Ā Ā Ā // find cube root of n Ā Ā Ā Ā int cubeRoot = ( int ) Math.pow(n, 1.0 / 3.0 ); Ā
Ā Ā Ā Ā // create an empty map Ā Ā Ā Ā HashMap<Integer, pair> s = new HashMap<Integer, pair>(); Ā
Ā Ā Ā Ā // Consider all pairs such with values less Ā Ā Ā Ā // than cuberoot Ā Ā Ā Ā for ( int x = 1 ; x < cubeRoot; x++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int y = x + 1 ; y <= cubeRoot; y++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find sum of current pair (x, y) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int sum = x*x*x + y*y*y; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // do nothing if sum is not equal to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // given number Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (sum != n) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen before, we found two pairs Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s.containsKey(sum)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print( "(" + s.get(sum).first+ ", " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + s.get(sum).second+ ") and (" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + x+ ", " + y+ ")" + "\n" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen for the first time Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s.put(sum, new pair(x, y)); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver code public static void main(String[] args) { Ā Ā Ā Ā int n = 13832 ; Ā Ā Ā Ā findPairs(n); } } Ā
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 program to find pairs # that can represent the given # number as sum of two cubes Ā
# Function to find pairs that # can represent the given number # as sum of two cubes def findPairs(n): Ā
Ā Ā Ā Ā # Find cube root of n Ā Ā Ā Ā cubeRoot = pow (n, 1.0 / 3.0 ); Ā Ā Ā Ā Ā Ā Ā # Create an empty map Ā Ā Ā Ā s = {} Ā Ā Ā Ā Ā Ā Ā # Consider all pairs such with Ā Ā Ā Ā # values less than cuberoot Ā Ā Ā Ā for x in range ( int (cubeRoot)): Ā Ā Ā Ā Ā Ā Ā Ā for y in range (x + 1 , Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int (cubeRoot) + 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Find sum of current pair (x, y) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum = x * x * x + y * y * y; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Do nothing if sum is not equal to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # given number Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( sum ! = n): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If sum is seen before, we Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # found two pairs Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if sum in s.keys(): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print ( "(" + str (s[ sum ][ 0 ]) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", " + str (s[ sum ][ 1 ]) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ") and (" + str (x) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ", " + str (y) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ")" + "\n" ) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # If sum is seen for the first time Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s[ sum ] = [x, y] Ā
# Driver code if __name__ = = "__main__" : Ā Ā Ā Ā Ā Ā Ā Ā Ā n = 13832 Ā Ā Ā Ā Ā Ā Ā Ā Ā findPairs(n) Ā Ā Ā Ā Ā # This code is contributed by rutvik_56 |
C#
// C# program to find pairs that can represent // the given number as sum of two cubes using System; using System.Collections.Generic; Ā
class GFG { Ā Ā Ā Ā class pair Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā public int first, second; Ā Ā Ā Ā Ā Ā Ā Ā public pair( int first, int second) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this .first = first; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this .second = second; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā // Function to find pairs that can represent // the given number as sum of two cubes static void findPairs( int n) { Ā Ā Ā Ā // find cube root of n Ā Ā Ā Ā int cubeRoot = ( int ) Math.Pow(n, 1.0/3.0); Ā Ā Ā Ā Ā Ā // create an empty map Ā Ā Ā Ā Dictionary< int , pair> s = new Dictionary< int , pair>(); Ā Ā Ā Ā Ā Ā // Consider all pairs such with values less Ā Ā Ā Ā // than cuberoot Ā Ā Ā Ā for ( int x = 1; x < cubeRoot; x++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int y = x + 1; y <= cubeRoot; y++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find sum of current pair (x, y) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int sum = x*x*x + y*y*y; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // do nothing if sum is not equal to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // given number Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (sum != n) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen before, we found two pairs Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s.ContainsKey(sum)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write( "(" + s[sum].first+ ", " Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + s[sum].second+ ") and (" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + x+ ", " + y+ ")" + "\n" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen for the first time Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s.Add(sum, new pair(x, y)); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā Ā // Driver code public static void Main(String[] args) { Ā Ā Ā Ā int n = 13832; Ā Ā Ā Ā findPairs(n); } } Ā
// This code is contributed by PrinciRaj1992 |
Javascript
// JavaScript program to find pairs that can represent // the given number as sum of two cubes Ā
// Function to find pairs that can represent // the given number as sum of two cubes function findPairs(n){ Ā Ā Ā Ā // find cube root of n Ā Ā Ā Ā let cubeRoot = Math.floor(Math.pow(n, 1/3)); Ā
Ā Ā Ā Ā // create an empty map Ā Ā Ā Ā let s = new Map(); Ā
Ā Ā Ā Ā // Consider all pairs such with values less Ā Ā Ā Ā // than cuberoot Ā Ā Ā Ā for (let x = 1; x < cubeRoot; x++){ Ā Ā Ā Ā Ā Ā Ā Ā for (let y = x + 1; y <= cubeRoot; y++){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find sum of current pair (x, y) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let sum = x*x*x + y*y*y; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // do nothing if sum is not equal to Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // given number Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (sum != n){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā continue ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen before, we found two pairs Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s.has(sum)){ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā console.log( "(" , s.get(sum)[0], "," , s.get(sum)[1], ") and (" ,x, "," ,y, ")" ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if sum is seen for the first time Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā s.set(sum, [x, y]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } } Ā
// Driver function { Ā Ā Ā Ā let n = 13832; Ā Ā Ā Ā findPairs(n); } Ā
// The code is contributed by Gautam goel (gautamgoel962) |
Output:Ā
(2, 24) and (18, 20)
Time Complexity of above solution is O(n2/3) which is much less than O(n).
Can we solve the above problem in O(n1/3) time? We will be discussing that in next post.
If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Ā
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!