📜  检查包含素数的数组的乘积是否是完全平方数

📅  最后修改于: 2021-10-27 08:08:47             🧑  作者: Mango

给定一个只包含素数的数组arr[] ,任务是检查数组元素的乘积是否是完全平方数。
例子:

朴素的方法:将数组的所有元素相乘,并检查乘积是否为完全平方数。
有效的方法:计算数组中所有元素的频率,如果所有元素的频率都是偶数,则产品将是一个完美的正方形,否则打印No。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if
// the product of all the array elements
// is a perfect square
bool isPerfectSquare(int arr[], int n)
{
    unordered_map umap;
 
    // Update the frequencies of
    // all the array elements
    for (int i = 0; i < n; i++)
        umap[arr[i]]++;
 
    unordered_map::iterator itr;
    for (itr = umap.begin(); itr != umap.end(); itr++)
 
        // If frequency of some element
        // in the array is odd
        if ((itr->second) % 2 == 1)
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 7, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    if (isPerfectSquare(arr, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
    // Function that returns true if
    // the product of all the array elements
    // is a perfect square
    static boolean isPerfectSquare(int [] arr, int n)
    {
         
        HashMap umap = new HashMap<>();
         
        // Update the frequencies of
        // all the array elements
        for (int i = 0; i < n; i++)
        {
            if(umap.containsKey(arr[i]))
                umap.put(arr[i], umap.get(arr[i]) + 1 );
            else
                umap.put(arr[i], 1);
             
        }
         
        Iterator >
                iterator = umap.entrySet().iterator();
 
        while(iterator.hasNext())
        {
            Map.Entry entry = iterator.next();
             
            // If frequency of some element
            // in the array is odd
            if (entry.getValue() % 2 == 1)
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void main (String[] args)
    {
 
        int arr [] = { 2, 2, 7, 7 };
        int n = arr.length;
     
        if (isPerfectSquare(arr, n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function that returns true if
# the product of all the array elements
# is a perfect square
def isPerfectSquare(arr, n) :
 
    umap = dict.fromkeys(arr, n);
 
    # Update the frequencies of
    # all the array elements
    for key in arr :
        umap[key] += 1;
 
    for key in arr :
 
        # If frequency of some element
        # in the array is odd
        if (umap[key] % 2 == 1) :
            return False;
 
    return True;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 2, 7, 7 ];
    n = len(arr)
 
    if (isPerfectSquare(arr, n)) :
        print("Yes");
    else :
        print("No");
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
    // Function that returns true if
    // the product of all the array elements
    // is a perfect square
    static bool isPerfectSquare(int [] arr, int n)
    {
         
        Dictionary umap = new Dictionary();
         
        // Update the frequencies of
        // all the array elements
        for (int i = 0; i < n; i++)
        {
            if(umap.ContainsKey(arr[i]))
                umap[arr[i]]++;
            else
                umap[arr[i]] = 1;
             
        }
         
        Dictionary.ValueCollection valueColl =
                                                umap.Values;
        foreach(int val in valueColl)
        {
            // If frequency of some element
            // in the array is odd
            if (val % 2 == 1)
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
 
        int [] arr = { 2, 2, 7, 7 };
        int n = arr.Length;
     
        if (isPerfectSquare(arr, n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
Yes