Smallest perfect cube in an array | 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
function checkPerfectcube ( n ) { let d = parseInt ( Math . cbrt ( n ) ) ; if ( d * d * d == n ) return true ; return false ; } function smallestPerfectCube ( a , n ) { let mini = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . min ( a [ i ] , mini ) ; } } return mini ; } let a = [ 16 , 8 , 25 , 2 , 3 , 10 ] ; let n = a . length ; document . write ( smallestPerfectCube ( a , n ) ) ;