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
function CheckArray ( arr , n ) { let prod = 1 ; let freq = [ ] ; for ( let i = 0 ; i < n ; ++ i ) { freq . push ( arr [ i ] ) ; prod *= arr [ i ] ; } let root = Math . floor ( Math . sqrt ( prod ) ) ; if ( root * root == prod ) { if ( freq . includes ( root ) & freq . lastIndexOf ( root ) != ( freq . length ) ) { return true ; } } return false ; } let arr = [ 1 , 2 , 12 , 3 , 2 ] ; let n = arr . length ; if ( CheckArray ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }