Range product queries in an array
Last Updated :
27 Apr, 2023
We have an array of integers and a set of range queries. For every query, we need to find product of elements in the given range.
Example:
Input : arr[] = {5, 10, 15, 20, 25}
queries[] = {(3, 5), (2, 2), (2, 3)}
Output : 7500, 10, 150
7500 = 15 x 20 x 25
10 = 10
150 = 10 x 15
A naive approach would be to navigate through each element in the array from lower to upper and multiply all the elements to find the product. Time complexity would be O(n) per query.
An efficient approach would be to take another array and store the product of all elements upto element with index i in the i’th index of the array. But there is a catch here. If any of the elements in the array is 0, the dp array will contain 0 from then onwards. Hence we should be getting 0 as our answer even if that 0 does not lie in the range between lower and upper.
To solve this case, we have used another array called countZeros which will contain the number of ‘0’s the array contains upto index i.
Now if countZeros[upper]-countZeros[lower] > 0, it signifies that there is a 0 in that range and hence the answer would be 0. Else we would proceed accordingly.
C++
#include<bits/stdc++.h>
using namespace std;
void findProduct( long arr[], int lower[],
int upper[], int n, int n1)
{
long preProd[n];
int countZeros[n];
long prod = 1;
int count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
count++;
else
prod *= arr[i];
preProd[i] = prod;
countZeros[i] = count;
}
for ( int i = 0; i < n1; i++)
{
int l = lower[i];
int u = upper[i];
if (l == 1)
{
if (countZeros[u - 1] == 0)
cout << (preProd[u - 1]) << endl;
else
cout<<0<<endl;
}
else
{
if (countZeros[u - 1] -
countZeros[l - 2] == 0)
cout << (preProd[u - 1] /
preProd[l - 2]) << endl;
else
cout << 0 << endl;
}
}
}
int main()
{
long arr[] ={ 0, 2, 3, 4, 5 };
int lower[] = {1, 2};
int upper[] = {3, 2};
findProduct(arr, lower, upper,5,2);
return 0;
}
|
Java
class Product {
static void findProduct( long [] arr, int [] lower,
int [] upper)
{
int n = arr.length;
long [] preProd = new long [n];
int [] countZeros = new int [n];
long prod = 1 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 0 )
count++;
else
prod *= arr[i];
preProd[i] = prod;
countZeros[i] = count;
}
for ( int i = 0 ; i < lower.length; i++) {
int l = lower[i];
int u = upper[i];
if (l == 1 )
{
if (countZeros[u - 1 ] == 0 )
System.out.println(preProd[u - 1 ]);
else
System.out.println( 0 );
}
else
{
if (countZeros[u - 1 ] - countZeros[l - 2 ] == 0 )
System.out.println(preProd[u - 1 ] / preProd[l - 2 ]);
else
System.out.println( 0 );
}
}
}
public static void main(String[] args)
{
long [] arr = new long [] { 0 , 2 , 3 , 4 , 5 };
int [] lower = { 1 , 2 };
int [] upper = { 3 , 2 };
findProduct(arr, lower, upper);
}
}
|
Python3
def findProduct(arr,lower, upper, n, n1):
preProd = [ 0 for i in range (n)]
countZeros = [ 0 for i in range (n)]
prod = 1
count = 0
for i in range ( 0 , n, 1 ):
if (arr[i] = = 0 ):
count + = 1
else :
prod * = arr[i]
preProd[i] = prod
countZeros[i] = count
for i in range ( 0 , n1, 1 ):
l = lower[i]
u = upper[i]
if (l = = 1 ):
if (countZeros[u - 1 ] = = 0 ):
print ( int (preProd[u - 1 ]))
else :
print ( 0 )
else :
if (countZeros[u - 1 ] -
countZeros[l - 2 ] = = 0 ):
print ( int (preProd[u - 1 ] /
preProd[l - 2 ]))
else :
print ( 0 )
if __name__ = = '__main__' :
arr = [ 0 , 2 , 3 , 4 , 5 ]
lower = [ 1 , 2 ]
upper = [ 3 , 2 ]
findProduct(arr, lower, upper, 5 , 2 )
|
C#
using System;
class GFG
{
static void findProduct( long [] arr,
int [] lower,
int [] upper)
{
int n = arr.Length;
long [] preProd = new long [n];
int [] countZeros = new int [n];
long prod = 1;
int count = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
count++;
else
prod *= arr[i];
preProd[i] = prod;
countZeros[i] = count;
}
for ( int i = 0;
i < lower.Length; i++)
{
int l = lower[i];
int u = upper[i];
if (l == 1)
{
if (countZeros[u - 1] == 0)
Console.WriteLine(preProd[u - 1]);
else
Console.WriteLine(0);
}
else
{
if (countZeros[u - 1] -
countZeros[l - 2] == 0)
Console.WriteLine(preProd[u - 1] /
preProd[l - 2]);
else
Console.WriteLine(0);
}
}
}
public static void Main()
{
long [] arr = {0, 2, 3, 4, 5};
int [] lower = {1, 2};
int [] upper = {3, 2};
findProduct(arr, lower, upper);
}
}
|
PHP
<?php
function findProduct( $arr , $lower , $upper )
{
$n = sizeof( $arr );
$preProd = array ( $n );
$countZeros = array ( $n );
$prod = 1;
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 0)
$count ++;
else
$prod *= $arr [ $i ];
$preProd [ $i ] = $prod ;
$countZeros [ $i ] = $count ;
}
for ( $i = 0; $i < sizeof( $lower ); $i ++) {
$l = $lower [ $i ];
$u = $upper [ $i ];
if ( $l == 1)
{
if ( $countZeros [ $u - 1] == 0)
echo ( $preProd [ $u - 1] );
else
echo (0);
}
else
{
if ( $countZeros [ $u - 1] - $countZeros [ $l - 2] == 0)
echo ( $preProd [ $u - 1] / $preProd [ $l - 2]);
else
echo (0);
}
echo "\n" ;
}
}
$arr = array (0, 2, 3, 4, 5 );
$lower = array (1, 2);
$upper = array (3, 2);
findProduct( $arr , $lower , $upper );
|
Javascript
<script>
function findProduct(arr, lower, upper)
{
let n = arr.length;
let preProd = new Array(n);
preProd.fill(0);
let countZeros = new Array(n);
countZeros.fill(0);
let prod = 1;
let count = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] == 0)
count++;
else
prod *= arr[i];
preProd[i] = prod;
countZeros[i] = count;
}
for (let i = 0;
i < lower.length; i++)
{
let l = lower[i];
let u = upper[i];
if (l == 1)
{
if (countZeros[u - 1] == 0)
document.write(preProd[u - 1] + "</br>" );
else
document.write(0 + "</br>" );
}
else
{
if (countZeros[u - 1] -
countZeros[l - 2] == 0)
document.write(
(preProd[u - 1] / preProd[l - 2]) +
"</br>" );
else
document.write(0 + "</br>" );
}
}
}
let arr = [0, 2, 3, 4, 5];
let lower = [1, 2];
let upper = [3, 2];
findProduct(arr, lower, upper);
</script>
|
Time complexity: O(n) during creation of dp array and O(1) per query
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...