Check if a number can be represented as product of two positive perfect cubes | Java program for the above approach ; Function to check if N can be represented as the product of two perfect cubes or not ; Stores the perfect cubes ; Traverse the Map ; Stores the first number ; Stores the second number ; Search the pair for the first number to obtain product N from the Map ; If N cannot be represented as the product of the two positive perfect cubes ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void productOfTwoPerfectCubes ( int N ) { Map < Integer , Integer > cubes = new HashMap < > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . put ( i * i * i , i ) ; for ( Map . Entry < Integer , Integer > itr : cubes . entrySet ( ) ) { int firstNumber = itr . getKey ( ) ; if ( N % itr . getKey ( ) == 0 ) { int secondNumber = N / itr . getKey ( ) ; if ( cubes . containsKey ( secondNumber ) ) { System . out . println ( " Yes " ) ; return ; } } } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 216 ; productOfTwoPerfectCubes ( N ) ; } }