Find the nearest odd and even perfect squares of odd and even array elements respectively | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestPerfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int sr = sqrt ( arr [ i ] ) ; if ( ( sr & 1 ) == ( arr [ i ] & 1 ) ) cout << sr * sr << " ▁ " ; else { sr ++ ; cout << sr * sr << " ▁ " ; } } } int main ( ) { int arr [ ] = { 6 , 3 , 2 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; nearestPerfectSquare ( arr , N ) ; return 0 ; }