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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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] *
    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 =
           
    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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


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