Check if a number can be represented as sum of two positive perfect cubes | Java program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; if it is same return true ; ; if the curr smaller than n increment the lo ; if the curr is greater than curr decrement the hi ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
import java . io . * ; class GFG { static boolean sumOfTwoCubes ( int n ) { int lo = 1 , hi = ( int ) Math . cbrt ( n ) ; while ( lo <= hi ) { int curr = ( lo * lo * lo + hi * hi * hi ) ; if ( curr == n ) return true ; if ( curr < n ) lo ++ ; else hi -- ; } return false ; } public static void main ( String [ ] args ) { int N = 28 ; if ( sumOfTwoCubes ( N ) ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } }