# 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

**Approach:** The problem can be easily solved using a prefix product array *prod[]* where the **prod[i]** stores the product of elements from **arr[0]** 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 <bits/stdc++.h> ` `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[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++) { ` ` ` `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[0]); ` ` ` ` ` `cout << findIndex(arr, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `findIndex(` `$a` `, ` `$n` `) ` `{ ` ` ` `$min_diff` `= PHP_INT_MAX; ` ` ` ` ` `// Prefix product array ` ` ` `$prod` `= ` `array` `(); ` ` ` `$prod` `[0] = ` `$a` `[0]; ` ` ` ` ` `// Compute the product array ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$prod` `[` `$i` `] = ` `$prod` `[` `$i` `- 1] * ` `$a` `[` `$i` `]; ` ` ` ` ` `// Iterate the product array to find the index ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` `$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 ` ` ` `$arr` `= ` `array` `( 3, 2, 5, 7, 2, 9 ); ` ` ` `$N` `= ` `count` `(` `$arr` `); ` ` ` ` ` `echo` `findIndex(` `$arr` `, ` `$N` `); ` ` ` ` ` `// This code is contributed by AnkitRai01 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Find minimum difference between any two elements
- Find minimum difference between any two elements | Set 2
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Find minimum value to assign all array elements so that array product becomes greater
- Product of all Subsequences of size K except the minimum and maximum Elements
- Program to find the maximum difference between the index of any two different numbers
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Find N integers with given difference between product and sum
- Minimize the difference between minimum and maximum elements
- Choose k array elements such that difference of maximum and minimum is minimized
- Minimum absolute difference of adjacent elements in a circular array
- Find set of m-elements with difference of any two elements is divisible by k
- Queries for number of distinct elements from a given index till last index in an array
- Number of permutations such that sum of elements at odd index and even index are equal
- Check if every index i has an index j such that sum of elements in both directions are equal

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.