Sparse Table Using JavaScript Array
Last Updated :
10 Oct, 2023
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));
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);
Similar Reads
JavaScript Program to Convert Byte Array to JSON In this article, we are going to learn about the conversion of a Byte Array into JSON. Converting a byte array to JSON means transforming a sequence of bytes into a structured JSON format, often involving decoding bytes to a text string and then parsing it into JSON data.Example:Input : [71, 101, 10
3 min read
JavaScript Program to Split Map Keys and Values into Separate Arrays In this article, we are going to learn about splitting map keys and values into separate arrays by using JavaScript. Splitting map keys and values into separate arrays refers to the process of taking a collection of key-value pairs stored in a map data structure and separating the keys and values in
5 min read
JavaScript - Access Elements in JS Array These are the following ways to Access Elements in an Array:1. Using Square Bracket NotationWe can access elements in an array by using their index, where the index starts from 0 for the first element. We can access using the bracket notation.JavaScriptconst a = [10, 20, 30, 40, 50]; const v = a[3];
2 min read
JavaScript Program to Create an Array with a Specific Length and Pre-filled Values In JavaScript, we can create an array with a specific length and pre-filled values using various approaches. This can be useful when we need to initialize an array with default or placeholder values before populating it with actual data.Table of ContentMethod 1: Using the Array() Constructor and fil
3 min read
Implement Custom Array Flat method in JavaScript The flat() method in JavaScript is used to flatten nested arrays. By using that function we can directly convert any type of array into 1D array. These are the following approaches to implementing a custom array flat method in JavaScript: Table of Content Using Recursion approachUsing Iterative appr
2 min read