Check if the array has an element which is equal to product of remaining elements | C ++ implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square ; then check if its square root exist in the array or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckArray ( int arr [ ] , int n ) { int prod = 1 ; unordered_set < int > freq ; for ( int i = 0 ; i < n ; ++ i ) { freq . insert ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = sqrt ( prod ) ; if ( root * root == prod ) if ( freq . find ( root ) != freq . end ( ) ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( CheckArray ( arr , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }