Find an index such that difference between product of elements before and after it is minimum | Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code
function solve ( array , N ) { let Arraynew = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . log ( array [ i ] ) ; } let prefixsum = new Array ( N ) ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } let answer = 0 ; let minabs = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( let i = 1 ; i < N - 1 ; i ++ ) { let ans1 = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } document . write ( " " + answer + " " ) ; } let array = [ 1 , 4 , 12 , 2 , 6 ] ; let N = 5 ; solve ( array , N ) ;