# 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 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.

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; ` `} `

## 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
```

My Personal Notes arrow_drop_up Coder Machine Learner Social Activist Vocalist

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.

Improved By : rituraj_jain, AnkitRai01