Find the nearest odd and even perfect squares of odd and even array elements respectively | 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 Given array
function nearestPerfectSquare ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let sr = Math . floor ( Math . sqrt ( arr [ i ] ) ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) document . write ( ( sr * sr ) + " " ) ; else { sr ++ ; document . write ( ( sr * sr ) + " " ) ; } } } let arr = [ 6 , 3 , 2 , 15 ] ; let N = arr . length ; nearestPerfectSquare ( arr , N ) ;