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
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void solve ( int Array [ ] , int N ) { double Arraynew [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = log ( Array [ i ] ) ; } double prefixsum [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } cout << " Index ▁ is : ▁ " << answer << endl ; } int main ( ) { int Array [ 5 ] = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; }