Largest perfect square number in an Array | Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
< ? php function checkPerfectSquare ( $ n ) { $ d = sqrt ( $ n ) ; if ( $ d * $ d == $ n ) return true ; return false ; } function largestPerfectSquareNumber ( $ a , $ n ) { $ maxi = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( checkPerfectSquare ( $ a [ $ i ] ) ) $ maxi = max ( $ a [ $ i ] , $ maxi ) ; } return $ maxi ; } $ a = array ( 16 , 20 , 25 , 2 , 3 , 10 ) ; $ n = count ( $ a ) ; echo largestPerfectSquareNumber ( $ a , $ n ) ; ? >