Smallest perfect cube in an array | C # 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
using System ; class GFG { static bool checkPerfectcube ( int n ) { int d = ( int ) Math . Sqrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int smallestPerfectCube ( int [ ] a , int n ) { int mini = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . Min ( a [ i ] , mini ) ; } } return mini ; } static public void Main ( ) { int [ ] a = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = a . Length ; Console . Write ( smallestPerfectCube ( a , n ) ) ; } }