Square Root (Sqrt) Decomposition Algorithm Using JavaScript Array Last Updated : 13 Sep, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how we can use the Square Root (Sqrt) Decomposition Algorithm using a JavaScript array. The square Root Decomposition Technique is one of the most common query optimization techniques used by competitive programmers. This technique helps us to reduce Time Complexity by a factor of sqrt(N). Approaches for Square Root (Sqrt) Decomposition Algorithm: Brute force Efficient approachSquare Root (Sqrt) Decomposition Algorithm Using Brute forceIn this approach, we simply iterate over each element in the range l to r and calculate its corresponding answer.Let’s consider these chunks or blocks as an individual array each of which contains sqrt(N) elements and you have computed your desired answer(according to your problem) individually for all the chunks.Now, you need to answer certain queries asking you the answer for the elements in the range l to r(l and r are starting and ending indices of the array) in the original n-sized array.Simply iterate over each element in the range l to r calculate its sum and return it for an answer. Example: JavaScript const MAXN = 20000; const arr = []; // Function for sqrt decomposition function query(l, r) { let sum = 0; for (let i = l; i <= r; i++) { sum += arr[i]; } return sum; } // Driver code function main() { const input = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; const n = input.length; arr.push(...input); console.log("query(3,8) : ", query(3, 8)); console.log("query(1,6) : ", query(1, 6)); arr[5] = 0; console.log("query(6,6) : ", query(6, 6)); } main(); Outputquery(3,8) : 27 query(1,6) : 39 query(6,6) : 4 Time Complexity: O(N) We can deal with these types of queries by summing the data from the completely overlapped decomposed blocks lying in the query range and then summing elements oneby one from the original array whose corresponding block is not completely overlapped by the query range. So the answer for the above query in the described image will be: ans = 5 + 2 + 11 + 3 = 21 Efficient approachIn this approach, we simply find the block in which the given index lies, then subtract its previous value and add the new updated value as per the point update query. We have constructed the decomposed array of sqrt(N) blocks and now we need to print the sum of elements in a given range. The range may totally cover the continuous sqrt blocks. So we can easily answer the sum of values in this range as the sum of completely overlapped blocks. Example: JavaScript let MAXN = 10000; let SQRSIZE = 100; let arr = new Array(MAXN); for (let i = 0; i < MAXN; i++) { arr[i] = 0; } let block = new Array(SQRSIZE); for (let i = 0; i < SQRSIZE; i++) { block[i] = 0; } let x; // Function for updating value // at specific index // of an array function update(idx, val) { let blockNumber = idx / x; block[blockNumber] += val - arr[idx]; arr[idx] = val; } function query(l, r) { let sum = 0; while (l < r && l % x != 0 && l != 0) { // traversing first block in range sum += arr[l]; l++; } while (l + x - 1 <= r) { // Traversing completely // overlapped blocks in range sum += block[l / x]; l += x; } while (l <= r) { // Traversing last block in range sum += arr[l]; l++; } return sum; } // Fills values in input[] function preprocess(input, n) { // Initiating block pointer let x = -1; // Calculating size of block x = Math.floor(Math.sqrt(n)); // Building the decomposed array for (let i = 0; i < n; i++) { arr[i] = input[i]; if (i % x == 0) { // Entering next block // Incrementing block pointer x++; } block[x] += arr[i]; } } let input = [1, 5, 2, 4, 6, 1, 3, 5, 7, 10]; let n = input.length; preprocess(input, n); console.log("query(3,7) : " + query(3, 7)); console.log("query(4,4) : " + query(4, 4)); update(6, 0); console.log("query(5,5) : " + query(5, 5)); Outputquery(3,7) : 19 query(4,4) : 6 query(5,5) : 1 Time Complexity: O(sqrt(N)) Auxiliary Space: O(MAXN), since MAXN extra space has been taken, where MAXN is the maximum value of N Comment More infoAdvertise with us Next Article Square Root (Sqrt) Decomposition Algorithm Using JavaScript Array N nikhilgarg527 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Program +1 More Similar Reads Largest Square Formed in a Matrix using JavaScript Given a matrix, our task is to find the largest square formed in a matrix using JavaScript. The largest square is a square that contains the same number of rows and columns and no more squares can be possible to form in that matrix that contains more rows and columns than this square. The below meth 3 min read Sparse Table Using JavaScript Array 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: Inp 3 min read JavaScript Program to Find the Area of a Square In this article, we will see how to find the area of a square in JavaScript if the side of the square is given. A square is a geometric shape with four equal sides and four right angles. The area of a square is calculated by multiplying the length of one side by itself. Formula of the Area of Square 3 min read JavaScript Program of Absolute Sum of Array Elements Using JavaScript, one can find the absolute sum of all elements present in an array. Below is an example to understand the problem clearly. Example:Input: [ -4, -7, 3, 10, 12] Output: 36 Explanation: Absolute values: 4 + 7 + 3 + 10 + 12 = 36 There are several approaches for finding the absolute sum 3 min read JavaScript Program to Search a Target Value in a Rotated Array In this article, we are going to implement a JavaScript program to Search for a target value in a rotated array. Table of Content Using Binary SearchUsing Linear SearchUsing Recursive FunctionUsing Modified Binary Search with Pivot Detection Using Binary SearchIn this approach, we begin by locating 5 min read JavaScript Program for Left Rotate by One in an Array In this article, we are going to learn about left Rotation by One in an Array by using JavaScript, Left rotation by one in an array means shifting all elements from one position to the left, with the last element moving to the first position. The order of elements is cyclically rotated while maintai 5 min read JavaScript Program to Find Median in a Stream of Integers (running integers) using Array We have given the integers which are read from the data stream. We need to find the median in a stream of integers also known as running integers using a JavaScript array. We will see various approaches to solve this problem in JavaScript. Below is the example which we help to understand the problem 6 min read JavaScript Program to Find Number of Squares in an N*N Grid Given a grid of side N * N, the task is to find the total number of squares that exist inside it. Examples: Input: N = 1 Output: 1 Input: N = 2 Output: 5 There are several approaches to finding the number of squares in an N*N grid which are as follows: Table of Content Using direct formulaIterative 3 min read Matrix Chain Multiplication by using JavaScript In this article, we are going to learn about Matrix Chain Multiplication by using JavaScript, Matrix Chain Multiplication refers to finding the most efficient way to multiply a sequence of matrices together, minimizing the total number of scalar multiplications required. Approaches to perform Matrix 2 min read JavaScript Program to Solve Quadratic Equation In this article, we are going to solve Quadratic equations with the help of JavaScript, A quadratic equation is a polynomial equation of degree 2, represented as ax2 + bx + c = 0. ax2 + bx + c = 0where a, b and c are real numbers and a â 0A quadratic equation's zeros, also known as roots, are the va 4 min read Like