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.

Input

Given array: [1, 2, 3, 4, 5, 6]

Output

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

The naive approach iterates through the array for each query to calculate the product of elements within the specified range [L,R]. It maintains a variable (ans) initialized to 1 and multiplies each element in the range to compute the result.

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);
The product of the elements in the given range 0 to 2 is: 6
The product of the elements in the given range 3 to 5 is: 120

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 queries increases it increase linearly which makes it not good 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 given 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);
The product of the elements in the given range 0 to 2 is: 6
The product of the elements in the given range 0 to 5 is: 42

Time and Space Complexity

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

Comparison

Feature Naive Approach Modular Multiplicative Inverse Approach
Concept Iterates through the array for each query to compute the product Uses pre-computed arrays for efficient query handling
Time Complexity (per query) O(N) O(1)
Space Complexity O(1) O(N)
Handling Large Numbers Risk of overflow with large products Computes products under modulo P to prevent overflow

Conclusion

In this tutorial, we have implemented JavaScript programs for calculating products of ranges in an array. The naive approach has O(N) time complexity per query while the modular inverse approach provides O(1) query time with preprocessing, making it efficient for multiple queries.

Updated on: 2026-03-15T23:19:01+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements