Open In App

Sparse Table Using JavaScript Array

Last Updated : 10 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about Sparse Table using JavaScript array. Sparse Table is a data structure in JavaScript used for efficient range queries (e.g., minimum or maximum) on an array. It precomputes and stores values to answer queries quickly, reducing time complexity.

Example:

Input:  arr[]   = {7, 2, 3, 0, 5, 10, 3, 12, 18};
query[] = [0, 4], [4, 7], [7, 8]
Output: Minimum of [0, 4] is 0
Minimum of [4, 7] is 3
Minimum of [7, 8] is 12

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Naive Sparse Table

In this approach, we are using the Naive Sparse Table approach, we create a 2D table to precompute and store minimum values for all subarrays in an input array. This allows efficient querying of minimum values for any subarray.

Example: In this example, we are using the above-explained apporach.

Javascript




function myFunction(arr) {
    const n = arr.length;
    const table = new Array(n).fill(0).map(() => 
        new Array(n).fill(0));
  
    // Initialize table with the original array
    for (let i = 0; i < n; i++) {
        table[i][i] = arr[i];
    }
  
    // Build the table using a nested loop
    for (let len = 2; len <= n; len++) {
        for (let i = 0; i + len - 1 < n; i++) {
            const j = i + len - 1;
            table[i][j] = Math.min(table[i][j - 1], 
                table[i + 1][j]);
        }
    }
  
    return table;
}
  
function sparseTable(table, left, right) {
    return table[left][right];
}
  
const arr = [33, 5, 4, 2, 6];
const result = myFunction(arr);
  
console.log(sparseTable(result, 1, 4));


Output

2

Apporach 2: Using Dynamic Programming

In this approach, we build a sparse table for efficient range minimum queries on an array. It precomputes indices of minimum elements, reducing query time. The query function returns the minimum value in a specified range.

Example: In this example, we creates a sparse table for an array to efficiently find minimum values in specific ranges. This technique precomputes data to speed up range query operations.

Javascript




function buildSparseTable(arr) {
    const n = arr.length;
    const table = new Array(n);
    let j = 0;
  
    for (const value of arr) {
        table[j] = 
            new Array(Math.floor(Math.log2(n)) + 1);
        table[j][0] = j;
        j++;
    }
  
    j = 1;
    for (let step = 1; (1 << j) <= n; j++, step = 1 << j) {
        for (let i = 0; i + step - 1 < n; i++) {
            const x = table[i][j - 1];
            const y = table[i + (step >> 1)][j - 1];
            table[i][j] = arr[x] < arr[y] ? x : y;
        }
    }
  
    return {
        table, logTable:
            new Array(n + 1).fill(0).map((_, i) => 
                Math.log2(i))
    };
}
  
function myFunction(sparseTable, left, right) {
    const { table, logTable } = sparseTable;
    const k = logTable[right - left + 1];
    const x = table[left][k];
    const y = table[right - (1 << k) + 1][k];
    return arr[x];
}
  
let arr = [33, 5, 4, 2, 6];
let sparseTable = buildSparseTable(arr);
  
// Query for the minimum value in the range [1, 4]
const result = myFunction(sparseTable, 1, 4);
console.log(result);


Output

2


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads