Check if product of array containing prime numbers is a perfect square | C ++ implementation of the approach ; Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int arr [ ] , int n ) { unordered_map < int , int > umap ; 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 ( ( itr -> second ) % 2 == 1 ) return false ; return true ; } 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 ; }