Check if the array has an element which is equal to product of remaining elements | 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
< ? php function CheckArray ( $ arr , $ n ) { $ prod = 1 ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { array_push ( $ freq , $ arr [ $ i ] ) ; $ prod *= $ arr [ $ i ] ; } $ freq = array_unique ( $ freq ) ; $ root = ( int ) ( sqrt ( $ prod ) ) ; if ( $ root * $ root == $ prod ) if ( in_array ( $ root , $ freq ) ) return true ; return false ; } $ arr = array ( 1 , 2 , 12 , 3 , 2 ) ; $ n = count ( $ arr ) ; if ( CheckArray ( $ arr , $ n ) ) echo " YES " ; else echo " NO " ; ? >