📜  不是理想正方形的最大数字

📅  最后修改于: 2021-05-04 17:56:10             🧑  作者: Mango

给定n个整数,找到最大的数字不是一个理想的平方。如果没有数字是完美的平方,则打印-1。
例子:

Input : arr[] = {16, 20, 25, 2, 3, 10| 
Output : 20 
Explanation: 20 is the largest number 
that is not a perfect square 

Input : arr[] = {36, 64, 10, 16, 29, 25| 
Output : 29 

正常的解决方案是对元素进行排序并对n个数字进行排序,然后使用sqrt()函数从后面开始检查一个非完美的平方数。从结尾开始的第一个数字(不是完美的平方数)是我们的答案。排序的复杂度为O(n log n) ,而sqrt()函数的复杂度为log n,因此在最坏的情况下,复杂度为O(n log n log n)。
有效的解决方案是使用O(n)中的遍历对所有元素进行迭代,并每次与最大元素进行比较,并存储所有非理想平方的最大值。储存的最大数量将是我们的答案。
下面是上述方法的说明:

CPP
// CPP program to find the largest non perfect
// square number among n numbers
#include 
using namespace std;
bool check(int n)
{
    // takes the sqrt of the number
    int d = sqrt(n);
 
    // checks if it is a perfect square number
    if (d * d == n)
        return true;
 
    return false;
}
 
// function to find the largest non perfect square number
int largestNonPerfectSquareNumber(int a[], int n)
{
    // stores the maximum of all non perfect square numbers
    int maxi = -1;
 
    // traverse for all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if not a perfect square
        if (!check(a[i]))
            maxi = max(a[i], maxi);
    }
    return maxi;
}
 
// driver code to check the above functions
int main()
{
    int a[] = { 16, 20, 25, 2, 3, 10 };
 
    int n = sizeof(a) / sizeof(a[0]);
    // function call
    cout << largestNonPerfectSquareNumber(a, n);
    return 0;
}


Java
// Java program to find the
// largest non perfect
// square number among n numbers
 
import java.io.*;
  
class GfG{
      
static Boolean check(int n)
{
    // takes the sqrt of the number
    int d = (int)Math.sqrt(n);
  
    // checks if it is a perfect square number
    if (d * d == n)
        return true;
  
    return false;
}
  
// function to find the largest
// non perfect square number
static int largestNonPerfectSquareNumber(int a[], int n)
{
    // stores the maximum of all
    // non perfect square numbers
    int maxi = -1;
  
    // traverse for all elements in the array
    for (int i = 0; i < n; i++) {
  
        // store the maximum if
        // not a perfect square
        if (!check(a[i]))
            maxi = Math.max(a[i], maxi);
    }
    return maxi;
}
 
    public static void main (String[] args) {
 
        int a[] = { 16, 20, 25, 2, 3, 10 };
        int n = a.length;
 
        // function call
        System.out.println(largestNonPerfectSquareNumber(a, n));
    }
}
 
// This code is contributed by Gitanjali.


Python3
# python program to find
# the largest non perfect
# square number among n numbers
 
import math
def check( n):
 
    # takes the sqrt of the number
    d = int(math.sqrt(n))
  
    # checks if it is a
    # perfect square number
    if (d * d == n):
        return True
  
    return False
 
  
# function to find the largest
# non perfect square number
def largestNonPerfectSquareNumber(a, n):
 
    # stores the maximum of all
    # non perfect square numbers
    maxi = -1
  
    # traverse for all elements
    # in the array
    for i in range(0,n):
  
        # store the maximum if
        # not a perfect square
        if (check(a[i])==False):
            maxi = max(a[i], maxi)
     
    return maxi
 
# driver code
a = [ 16, 20, 25, 2, 3, 10 ]
n= len(a)
 
# function call
print (largestNonPerfectSquareNumber(a, n))
 
# This code is contributed by Gitanjali.


C#
// C# program to find the largest non perfect
// square number among n numbers
using System;
 
class GfG {
     
    static bool check(int n)
    {
         
        // takes the sqrt of the number
        int d = (int)Math.Sqrt(n);
     
        // checks if it is a perfect
        // square number
        if (d * d == n)
            return true;
     
        return false;
    }
     
    // function to find the largest
    // non perfect square number
    static int largestNonPerfectSquareNumber(
                               int []a, int n)
    {
         
        // stores the maximum of all
        // non perfect square numbers
        int maxi = -1;
     
        // traverse for all elements in
        // the array
        for (int i = 0; i < n; i++) {
     
            // store the maximum if
            // not a perfect square
            if (!check(a[i]))
                maxi = Math.Max(a[i], maxi);
        }
         
        return maxi;
    }
 
    // driver code to check the above functions
    public static void Main ()
    {
 
        int []a = { 16, 20, 25, 2, 3, 10 };
        int n = a.Length;
 
        // function call
        Console.WriteLine(
           largestNonPerfectSquareNumber(a, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

20

可以将时间复杂度视为O(n),因为sqrt()函数可以在O(1)时间内实现用于固定大小(32位或64位)整数的整数[有关详细信息,请参阅Wiki]

?list = PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p