Check if product of array containing prime numbers is a perfect square
Given an array arr[] containing only prime numbers, the task is to check if the product of the array elements is a perfect square or not.
Examples:
Input: arr[] = {2, 2, 7, 7}
Output: Yes
2 * 2 * 7 * 7 = 196 = 142
Input: arr[] = {3, 3, 3, 5, 5}
Output: No
Naive approach: Multiply all the elements of the array and check whether the product is a perfect square or not.
Efficient approach: Count the frequencies of all the elements of the array, if frequency of all the elements are even then the product will be a perfect square else print No.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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<int, int> umap; // Update the frequencies of // all the array elements for (int i = 0; i < n; i++) umap[arr[i]]++; unordered_map<int, int>::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<Integer, Integer> 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<Map.Entry<Integer, Integer> > iterator = umap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<Integer, Integer> 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<int, int> umap = new Dictionary<int, int>(); // 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<int, int>.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 |
Yes
Recommended Posts:
- Check if each element of the given array is the product of exactly K prime numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count numbers upto N which are both perfect square and perfect cube
- Check if a number is perfect square without finding square root
- Product of all prime numbers in an Array
- Sum and product of k smallest and k largest prime numbers in the array
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Check if given number is perfect square
- Print n numbers such that their sum is a perfect square
- Check perfect square using addition/subtraction
- Check whether the number can be made perfect square after adding 1
- Check if elements of array can be made equal by multiplying given prime numbers
- Largest perfect square number in an Array
- Count of pairs in an array whose sum is a perfect square
- Smallest perfect square divisible by all elements of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



