Find Cube Pairs | Set 1 ( A n ^ ( 2 / 3 ) Solution ) | Java 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 ; find cube root of n ; create an empty map ; Consider all pairs such with values less than cuberoot ; find sum of current pair ( x , y ) ; do nothing if sum is not equal to given number ; if sum is seen before , we found two pairs ; if sum is seen for the first time ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; HashMap < Integer , pair > s = new HashMap < Integer , pair > ( ) ; for ( int x = 1 ; x < cubeRoot ; x ++ ) { for ( int y = x + 1 ; y <= cubeRoot ; y ++ ) { int sum = x * x * x + y * y * y ; if ( sum != n ) continue ; if ( s . containsKey ( sum ) ) { System . out . print ( " ( " + s . get ( sum ) . first + " , ▁ " + s . get ( sum ) . second + " ) ▁ and ▁ ( " + x + " , ▁ " + y + " ) " + "NEW_LINE"); } else s . put ( sum , new pair ( x , y ) ) ; } } } public static void main ( String [ ] args ) { int n = 13832 ; findPairs ( n ) ; } }