Find the nearest odd and even perfect squares of odd and even array elements respectively

Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity.
Examples:
Input: arr[ ] = {6, 3, 2, 15}
Output: 4 1 4 9
Explanation:
The nearest even perfect square of arr[0] (= 6) is 4.
The nearest odd perfect square of arr[1] (= 3) is 1.
The nearest even perfect square of arr[2] (= 2) is 4
The nearest odd perfect square of arr[3] (= 15) is 9.Input: arr[ ] = {31, 18, 64}
Output: 25 16 64
Approach: Follow the steps below to solve the problem:
- Traverse the array and perform the following operations:
- Find the square root of the current array element and store it in a variable, say sr.
- If sr is of same parity as arr[i], then sr*sr is the nearest perfect square.
- Otherwise, (sr + 1)2 is the nearest perfect square.
- Print the nearest perfect square obtained in the above step.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the nearest even and odd// perfect squares for even and odd array elementsvoid nearestPerfectSquare(int arr[], int N){Â
    // Traverse the array    for (int i = 0; i < N; i++) {Â
        // Calculate square root of        // current array element        int sr = sqrt(arr[i]);Â
        // If both are of same parity        if ((sr & 1) == (arr[i] & 1))            cout << sr * sr << " ";Â
      // Otherwise        else {            sr++;            cout << sr * sr << " ";        }    }}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 6, 3, 2, 15 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â Â Â Â nearestPerfectSquare(arr, N);Â Â Â Â return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;Â
class GFG{Â
// Function to find the nearest even and odd// perfect squares for even and odd array elementsstatic void nearestPerfectSquare(int arr[], int N){Â
    // Traverse the array    for (int i = 0; i < N; i++) {Â
        // Calculate square root of        // current array element        int sr = (int)Math.sqrt(arr[i]);Â
        // If both are of same parity        if ((sr & 1) == (arr[i] & 1))            System.out.print((sr * sr) + " ");Â
      // Otherwise        else {            sr++;            System.out.print((sr * sr) + " ");        }    }}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int arr[] = { 6, 3, 2, 15 };Â Â Â Â int N = arr.length;Â Â Â Â nearestPerfectSquare(arr, N);}}Â
// This code is contributed by souravghosh0416. |
Python3
# Python3 program for the above approachimport mathÂ
# Function to find the nearest even and odd# perfect squares for even and odd array elementsdef nearestPerfectSquare(arr, N) :Â
    # Traverse the array    for i in range(N):Â
        # Calculate square root of        # current array element        sr = int(math.sqrt(arr[i]))Â
        # If both are of same parity        if ((sr & 1) == (arr[i] & 1)) :            print(sr * sr, end = " ")Â
      # Otherwise        else :            sr += 1            print(sr * sr, end = " ")         # Driver Codearr = [ 6, 3, 2, 15 ]N = len(arr)nearestPerfectSquare(arr, N)Â
# This code is contributed by sanjoy_62. |
C#
// C# program for the above approachusing System;Â
class GFG{Â
  // Function to find the nearest even and odd  // perfect squares for even and odd array elements  static void nearestPerfectSquare(int[] arr, int N)  {Â
    // Traverse the array    for (int i = 0; i < N; i++) {Â
      // Calculate square root of      // current array element      int sr = (int)Math.Sqrt(arr[i]);Â
      // If both are of same parity      if ((sr & 1) == (arr[i] & 1))        Console.Write((sr * sr) + " ");Â
      // Otherwise      else {        sr++;        Console.Write((sr * sr) + " ");      }    }  }     // Driver Code  static public void Main()  {    int[] arr = { 6, 3, 2, 15 };    int N = arr.Length;    nearestPerfectSquare(arr, N);  }}Â
// This code is contributed by splevel62. |
Javascript
<script>Â
// Javascript program to implement// the above approachÂ
// Function to find the nearest even and odd// perfect squares for even and odd array elementsfunction nearestPerfectSquare(arr, N){         // Traverse the array    for(let i = 0; i < N; i++)     {                 // Calculate square root of        // current array element        let sr = Math.floor(Math.sqrt(arr[i]));Â
        // If both are of same parity        if ((sr & 1) == (arr[i] & 1))            document.write((sr * sr) + " ");Â
        // Otherwise        else        {            sr++;            document.write((sr * sr) + " ");        }    }}Â
// Driver code     // Given arraylet arr = [ 6, 3, 2, 15 ];let N = arr.length;Â
nearestPerfectSquare(arr, N);Â
// This code is contributed by target_2Â
</script> |
Output:Â
4 1 4 9
Â
Time Complexity: O(N*logN) where N is size of given array
Auxiliary Space: O(1)Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


