Find the nearest odd and even perfect squares of odd and even array elements respectively | Java program to implement the above approach ; Function to find the nearest even and odd perfect squares for even and odd array elements ; Traverse the array ; Calculate square root of current array element ; If both are of same parity ; Otherwise ; Driver Code
import java . util . * ; class GFG { static void nearestPerfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = ( int ) Math . sqrt ( arr [ i ] ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) System . out . print ( ( sr * sr ) + " ▁ " ) ; else { sr ++ ; System . out . print ( ( sr * sr ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 2 , 15 } ; int N = arr . length ; nearestPerfectSquare ( arr , N ) ; } }