Largest perfect square number in an Array | Java program to find the largest perfect square number among n numbers ; 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
import java . lang . Math ; import java . io . * ; class GFG { static boolean checkPerfectSquare ( double n ) { double d = Math . sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } static int largestPerfectSquareNumber ( int a [ ] , double n ) { int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } public static void main ( String [ ] args ) { int [ ] a = { 16 , 20 , 25 , 2 , 3 , 10 } ; double n = a . length ; System . out . println ( largestPerfectSquareNumber ( a , n ) ) ; } }