Find the nearest perfect square for each element of the array
Last Updated :
13 Sep, 2021
Given an array arr[] consisting of N positive integers, the task is to print the nearest perfect square for each array element.
Examples:
Input: arr[] = {5, 2, 7, 13}
Output: 4 1 9 16
Explanation:
The nearest perfect square of arr[0] (= 5) is 4.
The nearest perfect square of arr[1] (= 2) is 1.
The nearest perfect square of arr[2] (= 7) is 9.
The nearest perfect square of arr[3] (= 13) is 16.
Input: arr[] = {31, 18, 64}
Output: 36 16 64
Approach: Follow the steps below to solve the problem:
- Traverse the array from left to right.
- For every array element, find the nearest perfect square
- If N is a perfect square then print N
- Else, find the first perfect square number > N and note its difference with N.
- Then, find the first perfect square number < N and note its difference with N.
- And print the perfect square resulting in the minimum of these two differences obtained
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void nearestPerfectSquare( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int sr = sqrt (arr[i]);
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((arr[i] - a) < (b - arr[i]))
cout << a << " " ;
else
cout << b << " " ;
}
}
int main()
{
int arr[] = { 5, 2, 7, 13 };
int N = sizeof (arr) / sizeof (arr[0]);
nearestPerfectSquare(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void nearestPerfectSquare( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++)
{
int sr = ( int )Math.sqrt(arr[i]);
int a = sr * sr;
int b = (sr + 1 ) * (sr + 1 );
if ((arr[i] - a) < (b - arr[i]))
System.out.print(a + " " );
else
System.out.print(b + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 5 , 2 , 7 , 13 };
int N = arr.length;
nearestPerfectSquare(arr, N);
}
}
|
Python3
import math
def nearestPerfectSquare(arr,N):
for i in range ( 0 ,N):
sr = math.floor(math.sqrt(arr[i]))
a = sr * sr
b = (sr + 1 ) * (sr + 1 )
if ((arr[i] - a) < (b - arr[i])):
print (a ,end = " " )
else :
print (b ,end = " " )
arr = [ 5 , 2 , 7 , 13 ]
N = len (arr)
nearestPerfectSquare(arr, N)
|
C#
using System;
class GFG
{
static void nearestPerfectSquare( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
int sr = ( int )Math.Sqrt(arr[i]);
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((arr[i] - a) < (b - arr[i]))
Console.Write(a + " " );
else
Console.Write(b + " " );
}
}
public static void Main()
{
int [] arr = { 5, 2, 7, 13 };
int N = arr.Length;
nearestPerfectSquare(arr, N);
}
}
|
Javascript
<script>
function nearestPerfectSquare(arr, N)
{
for (let i = 0; i < N; i++) {
let sr = parseInt(Math.sqrt(arr[i]));
let a = sr * sr;
let b = (sr + 1) * (sr + 1);
if ((arr[i] - a) < (b - arr[i]))
document.write(a + " " );
else
document.write(b + " " );
}
}
let arr = [ 5, 2, 7, 13 ];
let N = arr.length;
nearestPerfectSquare(arr, N);
</script>
|
Time Complexity: O(N * sqrt(arr[i]))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...