Range product queries in an array

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 = 10 x 15 x 20
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for array 
// range product queries
#include<bits/stdc++.h>
using namespace std;
  
// Answers product queries
// given as two arrays 
// lower[] and upper[]. 
void findProduct(long arr[], int lower[],
                 int upper[], int n, int n1)
{
    long preProd[n];
    int countZeros[n];
  
    long prod = 1; // stores the product
  
    // keeps count of zeros
    int count = 0;
    for (int i = 0; i < n; i++) 
    {
  
        // if arr[i] is 0, we increment 
        // count and do not multiply
        // it with the product
        if (arr[i] == 0)
            count++;
        else
            prod *= arr[i];
  
        // store the value of prod in dp
        preProd[i] = prod;
  
        // store the value of
        // count in countZeros
        countZeros[i] = count;
    }
  
    // We have preprocessed
    // the array, let us
    // answer queries now.
    for (int i = 0; i < n1; i++) 
    {
        int l = lower[i];
        int u = upper[i];
  
        // range starts from
        // first element
        if (l == 1) 
        {
            // range does not 
            // contain any zero
            if (countZeros[u - 1] == 0)
                cout << (preProd[u - 1]) << endl;
            else
                cout<<0<<endl;
        }
  
        else // range starts from 
             // any other index
        {
            // no difference in countZeros 
            // indicates that there are no
            // zeros in the range
            if (countZeros[u - 1] - 
                countZeros[l - 2] == 0)
                cout << (preProd[u - 1] / 
                         preProd[l - 2]) << endl;
  
            else // zeros are present in the range
                cout << 0 << endl;
        }
    }
}
  
// Driver code
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;
}
  
// This code is contributed
// by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for array range product queries
class Product {
  
    // Answers product queries given as two arrays 
    // lower[] and upper[].    
    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; // stores the product
  
        // keeps count of zeros
        int count = 0;
        for (int i = 0; i < n; i++) {
  
            // if arr[i] is 0, we increment count and
            // do not multiply it with the product
            if (arr[i] == 0)
                count++;
            else
                prod *= arr[i];
  
            // store the value of prod in dp
            preProd[i] = prod;
  
            // store the value of count in countZeros
            countZeros[i] = count;
        }
  
        // We have preprocessed the array, let us
        // answer queries now.
        for (int i = 0; i < lower.length; i++) {
            int l = lower[i];
            int u = upper[i];
  
            // range starts from first element
            if (l == 1
            {
                // range does not contain any zero
                if (countZeros[u - 1] == 0)
                    System.out.println(preProd[u - 1]);
                else
                    System.out.println(0);
            }
  
            else // range starts from any other index
            {
                // no difference in countZeros indicates that
                // there are no zeros in the range
                if (countZeros[u - 1] - countZeros[l - 2] == 0)
                    System.out.println(preProd[u - 1] / preProd[l - 2]);
  
                else // zeros are present in the range
                    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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for array 
// range product queries
using System;
  
class GFG 
{
  
// Answers product queries
// given as two arrays 
// lower[] and upper[]. 
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; // stores the product
  
    // keeps count of zeros
    int count = 0;
    for (int i = 0; i < n; i++)
    {
  
        // if arr[i] is 0, we increment 
        // count and do not multiply it
        // with the product
        if (arr[i] == 0)
            count++;
        else
            prod *= arr[i];
  
        // store the value 
        // of prod in dp
        preProd[i] = prod;
  
        // store the value of 
        // count in countZeros
        countZeros[i] = count;
    }
  
    // We have preprocessed
    // the array, let us
    // answer queries now.
    for (int i = 0; 
             i < lower.Length; i++) 
    {
        int l = lower[i];
        int u = upper[i];
  
        // range starts from
        // first element
        if (l == 1) 
        {
            // range does not 
            // contain any zero
            if (countZeros[u - 1] == 0)
                Console.WriteLine(preProd[u - 1]);
            else
                Console.WriteLine(0);
        }
      
        // range starts from
        // any other index
        else 
        {
            // no difference in countZeros 
            // indicates that there are no
            // zeros in the range
            if (countZeros[u - 1] - 
                countZeros[l - 2] == 0)
                Console.WriteLine(preProd[u - 1] / 
                                  preProd[l - 2]);
  
            // zeros are present 
            // in the range
            else 
                Console.WriteLine(0);
        }
    }
}
  
// Driver Code
public static void Main()
{
    long[] arr = {0, 2, 3, 4, 5};
    int[] lower = {1, 2};
    int[] upper = {3, 2}; 
    findProduct(arr, lower, upper);
}
}
  
// This code is contributed
// by chandan_jnu.

chevron_right


Output:

0
2

Time complexity: O(n) during creation of dp array and O(1) per query



My Personal Notes arrow_drop_up

IT undergrad

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 : Chandan_Kumar, andrew1234



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.