text: Find Cube Pairs | Set 2 ( A n ^ ( 1 / 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 a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube [ ] whose sum is equal to n ; Driver Code
code: import java . io . * ; class GFG { static void findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; int cube [ ] = new int [ cubeRoot + 1 ] ; for ( int i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; int l = 1 ; int r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { System . out . println ( " ( " + l + " , ▁ " + r + " ) " ) ; l ++ ; r -- ; } } } public static void main ( String [ ] args ) { int n = 20683 ; findPairs ( n ) ; } }