Smallest perfect cube in an array | Java implementation of the approach ; Function that returns true if n is a perfect cube ; Takes the sqrt of the number ; Checks if it is a perfect cube number ; Function to return the smallest perfect cube from the array ; Stores the minimum of all the perfect cubes from the array ; Traverse all elements in the array ; Store the minimum if current element is a perfect cube ; Driver code
import java . io . * ; class GFG { static boolean checkPerfectcube ( int n ) { int d = ( int ) Math . cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int smallestPerfectCube ( int a [ ] , int n ) { int mini = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . min ( a [ i ] , mini ) ; } } return mini ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = a . length ; System . out . print ( smallestPerfectCube ( a , n ) ) ; } }