Find the nearest odd and even perfect squares of odd and even array elements respectively | Python3 program for 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 math NEW_LINE def nearestPerfectSquare ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT sr = int ( math . sqrt ( arr [ i ] ) ) NEW_LINE if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) : NEW_LINE INDENT print ( sr * sr , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT sr += 1 NEW_LINE print ( sr * sr , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 6 , 3 , 2 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE nearestPerfectSquare ( arr , N ) NEW_LINE