# Find an index such that difference between product of elements before and after it is minimum

Given an integer arr[], the task is to find an index such that the difference between the product of elements up to that index (including that index) and the product of rest of the elements is minimum. If more than one such index is present, then return the minimum index as the answer.

Examples:

Input : arr[] = { 2, 2, 1 }
Output : 0
For index 0: abs((2) – (2 * 1)) = 0
For index 1: abs((2 * 2) – (1)) = 3

Input : arr[] = { 3, 2, 5, 7, 2, 9 }
Output : 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple Solution is to traverse through all elements starting from first to second last element. For every element, find the product of elements till this element (including this element). Then find the product of elements after it. Finally compute the difference. If the difference is minimum so far, update the result.

Better Approach: The problem can be easily solved using a prefix product array prod[] where the prod[i] stores the product of elements from arr to arr[i]. Therefore, the product of rest of the elements can be easily found by dividing the total product of the array by the product up to current index. Now, iterate the product array to find the index with minimum difference.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define ll long long int ` ` `  `// Function to return the index i such that ` `// the absolute difference between product ` `// of elements up to that index and the ` `// product of rest of the elements ` `// of the array is minimum ` `int` `findIndex(``int` `a[], ``int` `n) ` `{ ` `    ``// To store the required index ` `    ``int` `res; ` ` `  `    ``ll min_diff = INT_MAX; ` ` `  `    ``// Prefix product array ` `    ``ll prod[n]; ` `    ``prod = a; ` ` `  `    ``// Compute the product array ` `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``prod[i] = prod[i - 1] * a[i]; ` ` `  `    ``// Iterate the product array to find the index ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` `        ``ll curr_diff = ``abs``((prod[n - 1] / prod[i]) - prod[i]); ` ` `  `        ``if` `(curr_diff < min_diff) { ` `            ``min_diff = curr_diff; ` `            ``res = i; ` `        ``} ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 2, 5, 7, 2, 9 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << findIndex(arr, N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG{ ` `     `  `// Function to return the index i such that  ` `// the absolute difference between product  ` `// of elements up to that index and the  ` `// product of rest of the elements  ` `// of the array is minimum  ` `static` `int` `findIndex(``int` `a[], ``int` `n)  ` `{  ` `    ``// To store the required index  ` `    ``int` `res = ``0``;  ` ` `  `    ``long` `min_diff = Long.MAX_VALUE;  ` ` `  `    ``// Prefix product array  ` `    ``long` `prod[] = ``new` `long``[n];  ` `    ``prod[``0``] = a[``0``];  ` ` `  `    ``// Compute the product array  ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``prod[i] = prod[i - ``1``] * a[i];  ` ` `  `    ``// Iterate the product array to find the index  ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `    ``{  ` `        ``long` `curr_diff = Math.abs((prod[n - ``1``] /  ` `                                   ``prod[i]) - prod[i]);  ` ` `  `        ``if` `(curr_diff < min_diff)  ` `        ``{  ` `            ``min_diff = curr_diff;  ` `            ``res = i;  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `res;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String arg[])  ` `{  ` `    ``int` `arr[] = { ``3``, ``2``, ``5``, ``7``, ``2``, ``9` `};  ` `    ``int` `N = arr.length;  ` ` `  `    ``System.out.println(findIndex(arr, N));  ` `}  ` `} ` ` `  `// This code is contributed by rutvik_56 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the index i such that  ` `# the absolute difference between product of  ` `# elements up to that index and the product of  ` `# rest of the elements of the array is minimum  ` `def` `findIndex(a, n):  ` `  `  `    ``# To store the required index  ` `    ``res, min_diff ``=` `None``, ``float``(``'inf'``)  ` ` `  `    ``# Prefix product array  ` `    ``prod ``=` `[``None``] ``*` `n  ` `    ``prod[``0``] ``=` `a[``0``]  ` ` `  `    ``# Compute the product array  ` `    ``for` `i ``in` `range``(``1``, n):  ` `        ``prod[i] ``=` `prod[i ``-` `1``] ``*` `a[i]  ` ` `  `    ``# Iterate the product array to find the index  ` `    ``for` `i ``in` `range``(``0``, n ``-` `1``):   ` `        ``curr_diff ``=` `abs``((prod[n ``-` `1``] ``/``/` `prod[i]) ``-` `prod[i])  ` ` `  `        ``if` `curr_diff < min_diff:   ` `            ``min_diff ``=` `curr_diff  ` `            ``res ``=` `i  ` `          `  `    ``return` `res  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` `  `  `    ``arr ``=` `[``3``, ``2``, ``5``, ``7``, ``2``, ``9``]   ` `    ``N ``=` `len``(arr)  ` ` `  `    ``print``(findIndex(arr, N)) ` ` `  `# This code is contributed by Rituraj Jain `

## PHP

 ` `

Output:

```2
```

Approach without overflow
The above solution might cause overflow. To prevent overflow problem, Take log of all the values of the array. Now, question is boiled down to divide array in two halves with absolute difference of sum is minimum possible. Now, array contains log values of elements at each index. Maintain a prefix sum array B which holds sum of all the values till index i. Check for all the indexes, abs(B[n-1] – 2*B[i]) and find the index with minimum possible absolute value.

## C++

 `#include ` `#define ll long long int ` `using` `namespace` `std; ` ` `  `// Function to find index ` `void` `solve(``int` `Array[], ``int` `N) ` `{ ` `    ``// Array to store log values of elements ` `    ``double` `Arraynew[N]; ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``Arraynew[i] = ``log``(Array[i]); ` `    ``} ` ` `  `    ``// Prefix Array to Maintain Sum of log values till index i ` `    ``double` `prefixsum[N]; ` `    ``prefixsum = Arraynew; ` ` `  `    ``for` `(``int` `i = 1; i < N; i++) { ` `        ``prefixsum[i] = prefixsum[i - 1] + Arraynew[i]; ` `    ``} ` ` `  `    ``// Answer Index ` `    ``int` `answer = 0; ` `    ``double` `minabs = ``abs``(prefixsum[N - 1] - 2 * prefixsum); ` ` `  `    ``for` `(``int` `i = 1; i < N - 1; i++) { ` `        ``double` `ans1 = ``abs``(prefixsum[N - 1] - 2 * prefixsum[i]); ` ` `  `        ``// Find minimum absolute value ` `        ``if` `(ans1 < minabs) { ` `            ``minabs = ans1; ` `            ``answer = i; ` `        ``} ` `    ``} ` ` `  `    ``cout << ``"Index is: "` `<< answer << endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `Array = { 1, 4, 12, 2, 6 }; ` `    ``int` `N = 5; ` `    ``solve(Array, N); ` `} `

## Python3

 `import` `math ` ` `  `# Function to find index ` `def` `solve( Array,  N): ` ` `  `    ``# Array to store log values of elements ` `    ``Arraynew ``=` `[``0``]``*``N ` `    ``for` `i ``in` `range``( N ) : ` `        ``Arraynew[i] ``=` `math.log(Array[i]) ` `     `  `  `  `    ``# Prefix Array to Maintain Sum of log values till index i ` `    ``prefixsum ``=` `[``0``]``*``N ` `    ``prefixsum[``0``] ``=` `Arraynew[``0``] ` `  `  `    ``for` `i ``in` `range``( ``1``,  N) : ` `        ``prefixsum[i] ``=` `prefixsum[i ``-` `1``] ``+` `Arraynew[i] ` `     `  `  `  `    ``# Answer Index ` `    ``answer ``=` `0` `    ``minabs ``=` `abs``(prefixsum[N ``-` `1``] ``-` `2` `*` `prefixsum[``0``]) ` `  `  `    ``for` `i ``in` `range``(``1``, N ``-` `1``): ` `        ``ans1 ``=` `abs``(prefixsum[N ``-` `1``] ``-` `2` `*` `prefixsum[i]) ` `  `  `        ``# Find minimum absolute value ` `        ``if` `(ans1 < minabs): ` `            ``minabs ``=` `ans1 ` `            ``answer ``=` `i ` `  `  `    ``print``(``"Index is: "` `,answer) ` `  `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``Array ``=` `[ ``1``, ``4``, ``12``, ``2``, ``6` `] ` `    ``N ``=` `5` `    ``solve(Array, N) ` ` `  `# This code is contributed by chitranayal `

Output:

```Index is: 2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.