Smallest index that splits an array into two subarrays with equal product | Function to find the smallest index that splits the array into two subarrays with equal product ; Stores the product of the array ; Traverse the given array ; Stores the product of left and the right subarrays ; Traverse the given array ; Update the products ; Check if product is equal ; Print resultant index ; If no partition exists , then print - 1. ; Driver Code
function prodEquilibrium ( arr , N ) { let product = 1 ; for ( let i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } let left = 1 ; let right = product ; for ( let i = 0 ; i < N ; i ++ ) { left = left * arr [ i ] ; right = right / arr [ i ] ; if ( left == right ) { document . write ( ( i + 1 ) + " " ) ; return ; } } document . write ( ( - 1 ) + " " ) ; } let arr = [ 1 , 2 , 3 , 3 , 2 , 1 ] ; let N = arr . length ; prodEquilibrium ( arr , N ) ;