# JavaScript Program for Products of ranges in an array

We will be given an array and we have to answer some queries related to the given range that is from a given starting index to the ending point or index we have to return the product of the elements in that range. We will see some approaches in this article to implement the above problem in JavaScript with the explanation.

## Introduction to Problem

We are given an array and some queries, in each query we will be given some ranges by indicating the first and the last index of the range and we have to answer the product of each element of the range.

For example −

Given array: 1 2 3 4 5 6
the product of range 0 to 2 is: 1 * 2 * 3 = 6.
The product of range 2 to 4 is: 3 * 4 * 5 = 60.

We will see two approaches to solving the problem first is the simple Naive approach and another is the mathematical modular inverse approach.

## Naive Approach

In this approach, we will simply traverse over the array, and for each query, we will maintain a variable to store the product.

### Example

Let’s see the code −

// function to find the range’s product
function rangeFun(arr, L, R){

// getting length of the array
var len = arr.length

// variable to maintain the result
var ans = 1

// traversing over the array in the given range
for(var i = L; i <= R; i++)    {
ans *= arr[i];
}
console.log("The product of the elements in the given range " + L + " to " + R + " is: " + ans )
}
// defining the array
var arr = [ 1, 2, 3, 4, 5, 6]

// calling the function in the range 0 to 2
rangeFun(arr, 0 ,2)

// calling the function in range 3 to 5
rangeFun(arr, 3 , 5)

### Time and Space Complexity

The time complexity of the above code is O(N), where N is the number of the elements in the given array and space complexity of the above code is O(1) as we are not using any extra space.

In the above code, time complexity is just O(N), but the thing is it is for the single query and as the number of quires increases it increase linearly which makes it not got enough to work with the N queries. To handle this issue, we will use the mathematical concept inverse modulation.

## Modular Multiplicative Inverse

We can utilise Modular Multiplicative Inverse since P is a prime. We may compute a pre-product array under modulo P using dynamic programming such that the value at index i comprises the product in the range [0, i]. In a similar manner, we may determine the pre-inverse product with respect to P. Now each query may be answered in O (1).

Because the product is computed modulo P, we cannot calculate the solution as Product[R]/Product[L-1]. There is always the danger of overflow if we do not calculate the product under modulo P.

### Example

Let’s see the code −

// defining some variable to work with
var max_length = 100;
var preProduct = new Array(max_length);
var inverseProduct = new Array(max_length);

// function to return the modulo inverse
function modInverse(a, m){
var m0 = m, t, q;
var x0 = 0
var x1 = 1
if (m == 1)    {
return 0;
}
while (a > 1)    {

// here q is the quotient
q = parseInt(a / m, 10);
t = m;

// m is remainder now, process

// same as Euclid's algo
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
// Make x1 positive
if (x1 < 0)    {
x1 += m0;
}
return x1;
}

// calculating pre_product of the givne array
function calculate_Pre_Product(arr, n, p){
preProduct[0] = arr[0];
for (var i = 1; i < n; i++)    {
preProduct[i] = preProduct[i - 1] * arr[i];
preProduct[i] = preProduct[i] % p;
}
}

// Calculating inverse_product of the given array
function calculate_inverse_product(arr, n, p){
inverseProduct[0] = modInverse(preProduct[0], p);
for (var i = 1; i < n; i++) {
inverseProduct[i] = modInverse(preProduct[i], p);
}
}

// defining the function to calculate Product

// in the given range.
function rangeFun(arr, L, R, p){
var ans = 0;
if (L == 0) {
ans = preProduct[R];
}
else{
ans = preProduct[R] * inverseProduct[L - 1];
}
console.log("The product of the elements in the given range " + L + " to " + R + " is: " + ans )
}

// defining the array
var arr = [ 1, 2, 3, 4, 5, 6 ];

// defining the modulo prime number
var p = 113;

// Calculating PreProduct and InverseProduct
calculate_Pre_Product(arr, arr.length, p);
calculate_inverse_product(arr, arr.length, p);
rangeFun(arr, 0 ,2, p)
rangeFun(arr, 0 , 5,  p)

### Time and Space Complexity

The time complexity of the above code is O(1), and the space complexity of the above code is O(N) as we are using N length two arrays to store the elements.

## Conclusion

In this tutorial, we have implemented a JavaScript article for products of ranges in an array. We have to answer some queries related to the given range and for that we have written two programs. First program was the naive approach with O(N) time complexity and the another approach is using the modular inverse concept with O(1) time complexity.

Updated on: 14-Apr-2023

75 Views