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
import math NEW_LINE def solve ( Array , N ) : NEW_LINE INDENT Arraynew = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT Arraynew [ i ] = math . log ( Array [ i ] ) NEW_LINE DEDENT prefixsum = [ 0 ] * N NEW_LINE prefixsum [ 0 ] = Arraynew [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] NEW_LINE DEDENT answer = 0 NEW_LINE minabs = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT ans1 = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) NEW_LINE if ( ans1 < minabs ) : NEW_LINE INDENT minabs = ans1 NEW_LINE answer = i NEW_LINE DEDENT DEDENT print ( " Index ▁ is : ▁ " , answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Array = [ 1 , 4 , 12 , 2 , 6 ] NEW_LINE N = 5 NEW_LINE solve ( Array , N ) NEW_LINE DEDENT