为数组的每个元素找到最近的完美正方形
给定一个由N个正整数组成的数组arr[] ,任务是为每个数组元素打印最接近的完美正方形。
例子:
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
解决方法:按照以下步骤解决问题:
- 从左到右遍历数组。
- 对于每个数组元素,找到最近的完美正方形
- 如果N是完美正方形,则打印 N
- 否则,找到第一个完全平方数> N并注意它与N的差异。
- 然后,找到第一个完全平方数< N并注意它与N的差异。
- 并打印得到这两个差值最小的完美正方形
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the nearest perfect square
// for every element in the given array
void nearestPerfectSquare(int arr[], int N)
{
// Traverse the array
for (int i = 0; i < N; i++) {
// Calculate square root of
// current element
int sr = sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest
if ((arr[i] - a) < (b - arr[i]))
cout << a << " ";
else
cout << b << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 5, 2, 7, 13 };
int N = sizeof(arr) / sizeof(arr[0]);
nearestPerfectSquare(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the nearest perfect square
// for every element in the given array
static void nearestPerfectSquare(int[] arr, int N)
{
// Traverse the array
for(int i = 0; i < N; i++)
{
// Calculate square root of
// current element
int sr = (int)Math.sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest
if ((arr[i] - a) < (b - arr[i]))
System.out.print(a + " ");
else
System.out.print(b + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 5, 2, 7, 13 };
int N = arr.length;
nearestPerfectSquare(arr, N);
}
}
// This code is contributed by souravmahato348
Python3
# Python program for the above approach
# Function to find the nearest perfect square
# for every element in the given array
# import the math module
import math
def nearestPerfectSquare(arr,N):
# Traverse the array
for i in range(0,N):
# Calculate square root of
# current element
sr = math.floor(math.sqrt(arr[i]))
# Calculate perfect square
a = sr * sr
b = (sr + 1) * (sr + 1)
# Find the nearest
if ((arr[i] - a) < (b - arr[i])):
print(a ,end=" ")
else :
print(b ,end=" ")
# Driver Code
arr = [5, 2, 7, 13]
N = len(arr)
nearestPerfectSquare(arr, N)
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the nearest perfect square
// for every element in the given array
static void nearestPerfectSquare(int[] arr, int N)
{
// Traverse the array
for (int i = 0; i < N; i++) {
// Calculate square root of
// current element
int sr = (int)Math.Sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest
if ((arr[i] - a) < (b - arr[i]))
Console.Write(a + " ");
else
Console.Write(b + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 5, 2, 7, 13 };
int N = arr.Length;
nearestPerfectSquare(arr, N);
}
}
// This code is contributed by souravmahato348
Javascript
输出:
4 1 9 16
时间复杂度: O(N * sqrt(arr[i]))
辅助空间: O(1)