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.
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
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[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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int findIndex( int a[], int n)
{
int res;
ll min_diff = INT_MAX;
ll prod[n];
prod[0] = a[0];
for ( int i = 1; i < n; i++)
prod[i] = prod[i - 1] * a[i];
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;
}
int main()
{
int arr[] = { 3, 2, 5, 7, 2, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findIndex(arr, N);
return 0;
}
|
Java
class GFG{
static int findIndex( int a[], int n)
{
int res = 0 ;
long min_diff = Long.MAX_VALUE;
long prod[] = new long [n];
prod[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
prod[i] = prod[i - 1 ] * a[i];
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;
}
public static void main(String arg[])
{
int arr[] = { 3 , 2 , 5 , 7 , 2 , 9 };
int N = arr.length;
System.out.println(findIndex(arr, N));
}
}
|
Python3
def findIndex(a, n):
res, min_diff = None , float ( 'inf' )
prod = [ None ] * n
prod[ 0 ] = a[ 0 ]
for i in range ( 1 , n):
prod[i] = prod[i - 1 ] * a[i]
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
if __name__ = = "__main__" :
arr = [ 3 , 2 , 5 , 7 , 2 , 9 ]
N = len (arr)
print (findIndex(arr, N))
|
C#
using System;
class GFG
{
static int findIndex( int [] a, int n)
{
int res = 0;
long min_diff = Int64.MaxValue;
long [] prod = new long [n];
prod[0] = a[0];
for ( int i = 1; i < n; i++)
prod[i] = prod[i - 1] * a[i];
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;
}
static void Main()
{
int [] arr = { 3, 2, 5, 7, 2, 9 };
int N = arr.Length;
Console.WriteLine(findIndex(arr, N));
}
}
|
PHP
<?php
function findIndex( $a , $n )
{
$min_diff = PHP_INT_MAX;
$prod = array ();
$prod [0] = $a [0];
for ( $i = 1; $i < $n ; $i ++)
$prod [ $i ] = $prod [ $i - 1] * $a [ $i ];
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 ;
}
$arr = array ( 3, 2, 5, 7, 2, 9 );
$N = count ( $arr );
echo findIndex( $arr , $N );
?>
|
Javascript
<script>
function findIndex(a, n)
{
let res = 0;
let min_diff = Number.MAX_VALUE;
let prod = new Array(n);
prod[0] = a[0];
for (let i = 1; i < n; i++)
prod[i] = prod[i - 1] * a[i];
for (let i = 0; i < n - 1; i++)
{
let curr_diff = Math.abs(parseInt(prod[n - 1] / prod[i], 10) - prod[i]);
if (curr_diff < min_diff)
{
min_diff = curr_diff;
res = i;
}
}
return res;
}
let arr = [ 3, 2, 5, 7, 2, 9 ];
let N = arr.length;
document.write(findIndex(arr, N));
</script>
|
Time Complexity: O(N), where n is the size of the given array.
Auxiliary Space: O(N), where n is the size of the given array.
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 <bits/stdc++.h>
#define ll long long int
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);
}
|
Java
public class Main
{
public static void solve( int Array[], int N)
{
double Arraynew[] = new double [N];
for ( int i = 0 ; i < N; i++) {
Arraynew[i] = Math.log(Array[i]);
}
double prefixsum[] = new double [N];
prefixsum[ 0 ] = Arraynew[ 0 ];
for ( int i = 1 ; i < N; i++)
{
prefixsum[i] = prefixsum[i - 1 ] + Arraynew[i];
}
int answer = 0 ;
double minabs = Math.abs(prefixsum[N - 1 ] - 2 *
prefixsum[ 0 ]);
for ( int i = 1 ; i < N - 1 ; i++)
{
double ans1 = Math.abs(prefixsum[N - 1 ] - 2 *
prefixsum[i]);
if (ans1 < minabs)
{
minabs = ans1;
answer = i;
}
}
System.out.println( "Index is: " + answer);
}
public static void main(String[] args)
{
int Array[] = { 1 , 4 , 12 , 2 , 6 };
int N = 5 ;
solve(Array, N);
}
}
|
Python3
import math
def solve( Array, N):
Arraynew = [ 0 ] * N
for i in range ( N ) :
Arraynew[i] = math.log(Array[i])
prefixsum = [ 0 ] * N
prefixsum[ 0 ] = Arraynew[ 0 ]
for i in range ( 1 , N) :
prefixsum[i] = prefixsum[i - 1 ] + Arraynew[i]
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])
if (ans1 < minabs):
minabs = ans1
answer = i
print ( "Index is: " ,answer)
if __name__ = = "__main__" :
Array = [ 1 , 4 , 12 , 2 , 6 ]
N = 5
solve(Array, N)
|
C#
using System;
using System.Collections;
class GFG{
public static void solve( int []Array, int N)
{
double []Arraynew = new double [N];
for ( int i = 0; i < N; i++)
{
Arraynew[i] = Math.Log(Array[i]);
}
double []prefixsum = new double [N];
prefixsum[0] = Arraynew[0];
for ( int i = 1; i < N; i++)
{
prefixsum[i] = prefixsum[i - 1] +
Arraynew[i];
}
int answer = 0;
double minabs = Math.Abs(prefixsum[N - 1] - 2 *
prefixsum[0]);
for ( int i = 1; i < N - 1; i++)
{
double ans1 = Math.Abs(prefixsum[N - 1] - 2 *
prefixsum[i]);
if (ans1 < minabs)
{
minabs = ans1;
answer = i;
}
}
Console.WriteLine( "Index is: " + answer);
}
public static void Main( string []args)
{
int []Array = { 1, 4, 12, 2, 6 };
int N = 5;
solve(Array, N);
}
}
|
Javascript
<script>
function solve(array, N)
{
let Arraynew = new Array(N);
for (let i = 0; i < N; i++)
{
Arraynew[i] = Math.log(array[i]);
}
let prefixsum = new Array(N);
prefixsum[0] = Arraynew[0];
for (let i = 1; i < N; i++)
{
prefixsum[i] = prefixsum[i - 1] +
Arraynew[i];
}
let answer = 0;
let minabs = Math.abs(prefixsum[N - 1] - 2 *
prefixsum[0]);
for (let i = 1; i < N - 1; i++)
{
let ans1 = Math.abs(prefixsum[N - 1] - 2 *
prefixsum[i]);
if (ans1 < minabs)
{
minabs = ans1;
answer = i;
}
}
document.write( "Index is: " + answer + "</br>" );
}
let array = [ 1, 4, 12, 2, 6 ];
let N = 5;
solve(array, N);
</script>
|
Time Complexity: O(N), where n is the size of the given array.
Auxiliary Space: O(N), where n is the size of the given array.
Contact Us