The Wayback Machine - https://web.archive.org/web/20240818165440/https://www.geeksforgeeks.org/square-root-sqrt-decomposition-algorithm/
Open In App

Square Root (Sqrt) Decomposition Algorithm

Last Updated : 16 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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) 

The key concept of this technique is to decompose a given array into small chunks specifically of size sqrt(N)

Follow the below steps to solve the problem:

  • We have an array of n elements and we decompose this array into small chunks of size sqrt(N)
  • We will be having exactly sqrt(N) such chunks provided that N is a perfect square
  • Therefore, now our array of N elements is decomposed into sqrt(N) blocks, where each block contains sqrt(N) elements (assuming the size of the array is a perfect square)
  • 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

Naive Approach: To solve the problem follow the below idea:

Simply iterate over each element in the range l to r and calculate its corresponding answer. Therefore, the Time Complexity per query will be O(N)

Below is the code for above approach :

C++




// C++ program to demonstrate working of simple
// array query using iteration
#include <bits/stdc++.h>
using namespace std;
 
#define MAXN 10000
 
int arr[MAXN]; // original array
 
// Time Complexity: O(r-l+1)
int query(int l, int r)
{
    int sum = 0;
    for (int i = l; i <= r; i++) {
        sum += arr[i];
    }
    return sum;
}
 
// Driver code
int main()
{
    // We have used separate array for input because
    // the purpose of this code is to explain simple
    // array query using iteration in competitive
    // programming where we have multiple inputs.
    int input[] = { 1, 5, 2, 4, 6, 1, 3, 5, 7, 10 };
    int n = sizeof(input) / sizeof(input[0]);
 
    // copying input[] to arr[]
    memcpy(arr, input, sizeof(input));
 
    cout << "query(3,8) : " << query(3, 8) << endl;
    cout << "query(1,6) : " << query(1, 6) << endl;
    arr[8] = 0; // updating arr[8] to 0
    cout << "query(8,8) : " << query(8, 8) << endl;
    return 0;
}


Java




import java.util.Arrays;
 
public class GFG{
    static int[] arr = new int[10000]; // original array
 
    // Time Complexity: O(r-l+1)
    static int query(int l, int r) {
        int sum = 0;
        for (int i = l; i <= r; i++) {
            sum += arr[i];
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args) {
        // We have used separate array for input because
        // the purpose of this code is to explain simple
        // array query using iteration in competitive
        // programming where we have multiple inputs.
        int[] input = {1, 5, 2, 4, 6, 1, 3, 5, 7, 10};
        int n = input.length;
 
        // copying input[] to arr[]
        System.arraycopy(input, 0, arr, 0, n);
 
        System.out.println("query(3,8) : " + query(3, 8));
        System.out.println("query(1,6) : " + query(1, 6));
        arr[8] = 0; // updating arr[8] to 0
        System.out.println("query(8,8) : " + query(8, 8));
    }
}


Python3




arr = [0] * 10000  # original array
 
# Time Complexity: O(r-l+1)
def query(l, r):
    _sum = 0
    for i in range(l, r + 1):
        _sum += arr[i]
    return _sum
 
# Driver code
if __name__ == '__main__':
    # We have used separate list for input because
    # the purpose of this code is to explain simple
    # array query using iteration in competitive
    # programming where we have multiple inputs.
    input_arr = [1, 5, 2, 4, 6, 1, 3, 5, 7, 10]
 
    # copying input_arr to arr
    arr[:len(input_arr)] = input_arr
 
    print("query(3,8) :", query(3, 8))
    print("query(1,6) :", query(1, 6))
    arr[8] = 0  # updating arr[8] to 0
    print("query(8,8) :", query(8, 8))


C#




using System;
 
class Program
{
    const int MAXN = 10000;
    static int[] arr = new int[MAXN]; // original array
 
    // Time Complexity: O(r-l+1)
    static int Query(int l, int r)
    {
        int sum = 0;
        for (int i = l; i <= r; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
 
    static void Main()
    {
        // We have used separate array for input because
        // the purpose of this code is to explain simple
        // array query using iteration in competitive
        // programming where we have multiple inputs.
        int[] input = { 1, 5, 2, 4, 6, 1, 3, 5, 7, 10 };
        int n = input.Length;
 
        // copying input[] to arr[]
        Array.Copy(input, arr, n);
 
        Console.WriteLine("query(3,8) : " + Query(3, 8));
        Console.WriteLine("query(1,6) : " + Query(1, 6));
        arr[8] = 0; // updating arr[8] to 0
        Console.WriteLine("query(8,8) : " + Query(8, 8));
    }
}
// This code is contributed by uomkar369


Javascript




// Function to calculate the sum of elements in the range [l, r] of the array arr
// Time Complexity: O(r - l + 1)
function query(l, r, arr) {
    let sum = 0;
    for (let i = l; i <= r; i++) {
        sum += arr[i];
    }
    return sum;
}
 
// Driver code
    // We have used a separate array for input because
    // the purpose of this code is to explain a simple
    // array query using iteration in competitive
    // programming where we have multiple inputs.
    const input = [1, 5, 2, 4, 6, 1, 3, 5, 7, 10];
    const n = input.length;
 
    // copying input[] to arr[]
    const arr = [...input];
 
    console.log("query(3,8) :", query(3, 8, arr));
    console.log("query(1,6) :", query(1, 6, arr));
    arr[8] = 0; // updating arr[8] to 0
    console.log("query(8,8) :", query(8, 8, arr));


Output

query(3,8) : 26
query(1,6) : 21
query(8,8) : 0




Efficient Approach(Sqrt Decomposition Trick): To solve the problem follow the below idea: 

As we have already precomputed the answer for all individual chunks and now we need to answer the queries in range l to r. Now we can simply combine the answers of the chunks that lie in between the range l to r in the original array. So, if we see carefully here we are jumping sqrt(N) steps at a time instead of jumping 1 step at a time as done in the naive approach. Let’s just analyze its Time Complexity and implementation considering the below problem:

Problem :

Given an array of n elements. We need to answer q 
queries telling the sum of elements in range l to 
r in the array. Also the array is not static i.e 
the values are changed via some point update query.

Range Sum Queries are of form : Q l r , 
where l is the starting index r is the ending index

Point update Query is of form : U idx val, 
where idx is the index to update val is the 
updated value

Below is the illustration of the above approach:

Let us consider that we have an array of 9 elements: A[] = {1, 5, 2, 4, 6, 1, 3, 5, 7}
1. Let’s decompose this array into sqrt(9) blocks, where each block will contain the sum of elements lying in it. Therefore now our decomposed array would look like this:
 

Sqrt decomposition of given array

2. Till now we have constructed the decomposed array of sqrt(9) blocks and now we need to print the sum of elements in a given range. 
So first let’s see two basic types of overlap that a range query can have on our array:

Range Query of type 1 (Given Range is on Block Boundaries) :

 

sqrt2

In this type the query, 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. 

So the answer for the above query in the described image will be: ans = 11 + 15 = 26 

Time Complexity: O(sqrt(N)). In the worst case, our range can be 0 to N-1(where N is the size of the array and assuming N to be a perfect square). In this case, all the blocks are completely overlapped by our query range. Therefore, to answer this query we need to iterate over all the decomposed blocks for the array and we know that the number of blocks = sqrt(N). Hence, the complexity for this type of query will be O(sqrt(N)) in the worst case.

Range Query of type 2 (Given Range is NOT on boundaries):

 

Query 3

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 one by 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 

Time Complexity: O(sqrt(N)). Let’s consider a query [l = 1 and r = n-2] (n is the size of the array and has 0-based indexing). Therefore, for this query exactly ( sqrt(n) – 2 ) blocks will be completely overlapped whereas the first and last blocks will be partially overlapped with just one element left outside the overlapping range. So, the completely overlapped blocks can be summed up in ( sqrt(n) – 2 ) ~ sqrt(n) iterations, whereas the first and last blocks are needed to be traversed one by one separately. But as we know that the number of elements in each block is at max sqrt(n), to sum up, the last block individually we need to make, 
(sqrt(n)-1) ~ sqrt(n) iterations and same for the last block. 

So, the overall Complexity = O(sqrt(n)) + O(sqrt(n)) + O(sqrt(n)) = O(3*sqrt(N)) = O(sqrt(N))
 

Update Queries(Point update):

In this query, 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. 

Time Complexity: O(1) 

Below is the implementation of the above approach:

C++




// C++ program to demonstrate working of Square Root
// Decomposition.
#include <bits/stdc++.h>
using namespace std;
 
#define MAXN 10000
#define SQRSIZE 100
 
int arr[MAXN]; // original array
int block[SQRSIZE]; // decomposed array
int blk_sz; // block size
 
// Time Complexity : O(1)
void update(int idx, int val)
{
    int blockNumber = idx / blk_sz;
    block[blockNumber] += val - arr[idx];
    arr[idx] = val;
}
 
// Time Complexity : O(sqrt(n))
int query(int l, int r)
{
    int sum = 0;
    while (l < r and l % blk_sz != 0 and l != 0) {
        // traversing first block in range
        sum += arr[l];
        l++;
    }
    while (l + blk_sz - 1 <= r) {
        // traversing completely overlapped blocks in range
        sum += block[l / blk_sz];
        l += blk_sz;
    }
    while (l <= r) {
        // traversing last block in range
        sum += arr[l];
        l++;
    }
    return sum;
}
 
// Fills values in input[]
void preprocess(int input[], int n)
{
    // initiating block pointer
    int blk_idx = -1;
 
    // calculating size of block
    blk_sz = sqrt(n);
 
    // building the decomposed array
    for (int i = 0; i < n; i++) {
        arr[i] = input[i];
        if (i % blk_sz == 0) {
            // entering next block
            // incrementing block pointer
            blk_idx++;
        }
        block[blk_idx] += arr[i];
    }
}
 
// Driver code
int main()
{
    // We have used separate array for input because
    // the purpose of this code is to explain SQRT
    // decomposition in competitive programming where
    // we have multiple inputs.
    int input[] = { 1, 5, 2, 4, 6, 1, 3, 5, 7, 10 };
    int n = sizeof(input) / sizeof(input[0]);
 
    preprocess(input, n);
 
    cout << "query(3,8) : " << query(3, 8) << endl;
    cout << "query(1,6) : " << query(1, 6) << endl;
    update(8, 0);
    cout << "query(8,8) : " << query(8, 8) << endl;
    return 0;
}


Java




// Java program to demonstrate working of
// Square Root Decomposition.
import java.util.*;
 
class GFG {
 
    static int MAXN = 10000;
    static int SQRSIZE = 100;
 
    static int[] arr = new int[MAXN]; // original array
    static int[] block
        = new int[SQRSIZE]; // decomposed array
    static int blk_sz; // block size
 
    // Time Complexity : O(1)
    static void update(int idx, int val)
    {
        int blockNumber = idx / blk_sz;
        block[blockNumber] += val - arr[idx];
        arr[idx] = val;
    }
 
    // Time Complexity : O(sqrt(n))
    static int query(int l, int r)
    {
        int sum = 0;
        while (l < r && l % blk_sz != 0 && l != 0) {
            // traversing first block in range
            sum += arr[l];
            l++;
        }
        while (l + blk_sz - 1 <= r) {
            // traversing completely
            // overlapped blocks in range
            sum += block[l / blk_sz];
            l += blk_sz;
        }
        while (l <= r) {
            // traversing last block in range
            sum += arr[l];
            l++;
        }
        return sum;
    }
 
    // Fills values in input[]
    static void preprocess(int input[], int n)
    {
        // initiating block pointer
        int blk_idx = -1;
 
        // calculating size of block
        blk_sz = (int)Math.sqrt(n);
 
        // building the decomposed array
        for (int i = 0; i < n; i++) {
            arr[i] = input[i];
            if (i % blk_sz == 0) {
                // entering next block
                // incrementing block pointer
                blk_idx++;
            }
            block[blk_idx] += arr[i];
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // We have used separate array for input because
        // the purpose of this code is to explain SQRT
        // decomposition in competitive programming where
        // we have multiple inputs.
        int input[] = { 1, 5, 2, 4, 6, 1, 3, 5, 7, 10 };
        int n = input.length;
 
        preprocess(input, n);
 
        System.out.println("query(3, 8) : " + query(3, 8));
        System.out.println("query(1, 6) : " + query(1, 6));
        update(8, 0);
        System.out.println("query(8, 8) : " + query(8, 8));
    }
}
 
// This code is contributed by PrinciRaj1992


Python 3




# Python 3 program to demonstrate working of Square Root
# Decomposition.
from math import sqrt
 
MAXN = 10000
SQRSIZE = 100
 
arr = [0]*(MAXN)         # original array
block = [0]*(SQRSIZE)     # decomposed array
blk_sz = 0                 # block size
 
# Time Complexity : O(1)
 
 
def update(idx, val):
    blockNumber = idx // blk_sz
    block[blockNumber] += val - arr[idx]
    arr[idx] = val
 
# Time Complexity : O(sqrt(n))
 
 
def query(l, r):
    sum = 0
    while (l < r and l % blk_sz != 0 and l != 0):
 
        # traversing first block in range
        sum += arr[l]
        l += 1
 
    while (l + blk_sz - 1 <= r):
 
        # traversing completely overlapped blocks in range
        sum += block[l//blk_sz]
        l += blk_sz
 
    while (l <= r):
 
        # traversing last block in range
        sum += arr[l]
        l += 1
 
    return sum
 
# Fills values in input[]
 
 
def preprocess(input, n):
 
    # initiating block pointer
    blk_idx = -1
 
    # calculating size of block
    global blk_sz
    blk_sz = int(sqrt(n))
 
    # building the decomposed array
    for i in range(n):
        arr[i] = input[i]
        if (i % blk_sz == 0):
 
            # entering next block
            # incrementing block pointer
            blk_idx += 1
 
        block[blk_idx] += arr[i]
 
# Driver code
 
 
# We have used separate array for input because
# the purpose of this code is to explain SQRT
# decomposition in competitive programming where
# we have multiple inputs.
input = [1, 5, 2, 4, 6, 1, 3, 5, 7, 10]
n = len(input)
 
preprocess(input, n)
 
print("query(3,8) : ", query(3, 8))
print("query(1,6) : ", query(1, 6))
update(8, 0)
print("query(8,8) : ", query(8, 8))
 
# This code is contributed by Sanjit_Prasad


C#




// C# program to demonstrate working of
// Square Root Decomposition.
using System;
 
class GFG {
    static int MAXN = 10000;
    static int SQRSIZE = 100;
 
    static int[] arr = new int[MAXN]; // original array
    static int[] block
        = new int[SQRSIZE]; // decomposed array
    static int blk_sz; // block size
 
    // Time Complexity : O(1)
    static void update(int idx, int val)
    {
        int blockNumber = idx / blk_sz;
        block[blockNumber] += val - arr[idx];
        arr[idx] = val;
    }
 
    // Time Complexity : O(sqrt(n))
    static int query(int l, int r)
    {
        int sum = 0;
        while (l < r && l % blk_sz != 0 && l != 0) {
            // traversing first block in range
            sum += arr[l];
            l++;
        }
        while (l + blk_sz - 1 <= r) {
            // traversing completely
            // overlapped blocks in range
            sum += block[l / blk_sz];
            l += blk_sz;
        }
        while (l <= r) {
            // traversing last block in range
            sum += arr[l];
            l++;
        }
        return sum;
    }
 
    // Fills values in input[]
    static void preprocess(int[] input, int n)
    {
        // initiating block pointer
        int blk_idx = -1;
 
        // calculating size of block
        blk_sz = (int)Math.Sqrt(n);
 
        // building the decomposed array
        for (int i = 0; i < n; i++) {
            arr[i] = input[i];
            if (i % blk_sz == 0) {
                // entering next block
                // incrementing block pointer
                blk_idx++;
            }
            block[blk_idx] += arr[i];
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        // We have used separate array for input because
        // the purpose of this code is to explain SQRT
        // decomposition in competitive programming where
        // we have multiple inputs.
        int[] input = { 1, 5, 2, 4, 6, 1, 3, 5, 7, 10 };
        int n = input.Length;
 
        preprocess(input, n);
 
        Console.WriteLine("query(3, 8) : " + query(3, 8));
        Console.WriteLine("query(1, 6) : " + query(1, 6));
        update(8, 0);
        Console.WriteLine("query(8, 8) : " + query(8, 8));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// Javascript program to demonstrate working of
// Square Root Decomposition.
 
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 blk_sz;    
 
// Time Complexity : O(1)
function update(idx,val)
{
    let blockNumber = idx / blk_sz;
    block[blockNumber] += val - arr[idx];
    arr[idx] = val;
}
 
// Time Complexity : O(sqrt(n))
function query(l, r)
{
    let sum = 0;
    while (l < r && l % blk_sz != 0 && l != 0)
    {
        // traversing first block in range
        sum += arr[l];
        l++;
    }
    while (l+blk_sz-1 <= r)
    {
        // traversing completely
        // overlapped blocks in range
        sum += block[l / blk_sz];
        l += blk_sz;
    }
    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 blk_idx = -1;
   
    // calculating size of block
    blk_sz = Math.floor( Math.sqrt(n));
   
    // building the decomposed array
    for (let i = 0; i < n; i++)
    {
        arr[i] = input[i];
        if (i % blk_sz == 0)
        {
         
            // entering next block
            // incrementing block pointer
            blk_idx++;
        }
        block[blk_idx] += arr[i];
    }
}
 
// Driver code
// We have used separate array for input because
    // the purpose of this code is to explain SQRT
    // decomposition in competitive programming where
    // we have multiple inputs.
    let input = [1, 5, 2, 4, 6, 1, 3, 5, 7, 10];
    let n = input.length;
   
    preprocess(input, n);
   
    document.write("query(3, 8) : " +
                        query(3, 8)+"<br>");
    document.write("query(1, 6) : " +
                        query(1, 6)+"<br>");
    update(8, 0);
    document.write("query(8, 8) : " +
                        query(8, 8)+"<br>");
 
// This code is contributed by rag2127
</script>


Output

query(3,8) : 26
query(1,6) : 21
query(8,8) : 0



Time Complexity: O(N)
Auxiliary Space: O(MAXN), since MAXN extra space has been taken, where MAXN is the maximum value of N

Note: The above code works even if N is not a perfect square. In this case, the last block will contain even less number of elements than sqrt(N), thus reducing the number of iterations. 
Let’s say n = 10. In this case, we will have 4 blocks first three blocks of size 3, and the last block of size 1.




Previous Article
Next Article

Similar Reads

Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)
Prerequisite: Introduction and DFSThe task is to find LCA of two given nodes in a tree (not necessarily a Binary Tree). In previous posts, we have seen how to calculate LCA using the Sparse Matrix DP approach. In this post, we will see an optimization done on the Naive method by sqrt decomposition technique that works well over the Naive Approach.
15+ min read
Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
Given a perfect square natural number N. The task is to find all the factors of N. Examples Input: N = 100 Output: 1 2 4 5 10 20 25 50 100 Input: N = 900 Output: 1 2 4 3 6 12 9 18 36 5 10 20 15 30 60 45 90 180 25 50 100 75 150 300 225 450 900 Approach: Find the square root of N in temp.Find all the prime factors of temp in O(sqrt(temp)) using the a
15+ min read
MO's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction)
Let us consider the following problem to understand MO's Algorithm.We are given an array and a set of query ranges, we are required to find the sum of every query range. Example: Input: arr[] = {1, 1, 2, 1, 3, 4, 5, 2, 8}; query[] = [0, 4], [1, 3] [2, 4] Output: Sum of arr[] elements in range [0, 4] is 8 Sum of arr[] elements in range [1, 3] is 4 S
15+ min read
Square root of a number without using sqrt() function
Given a number N, the task is to find the square root of N without using sqrt() function. Examples: Input: N = 25 Output: 5 Input: N = 3 Output: 1.73205 Input: N = 2.5 Output: 1.58114 Approach 1: We can consider (?x-?x)2 = 0.Replacing one of the ?x's with y, then the equation becomes (y-?x)2 =&gt; y2 - 2y?x + x = 0=&gt; ?x = (y2 + x) / 2y=&gt; ?x =
14 min read
Floor square root without using sqrt() function : Recursive
Given a number N, the task is to find the floor square root of the number N without using the built-in square root function. Floor square root of a number is the greatest whole number which is less than or equal to its square root. Examples: Input: N = 25 Output: 5 Explanation: Square root of 25 = 5. Therefore 5 is the greatest whole number less th
7 min read
Range Minimum Query (Square Root Decomposition and Sparse Table)
We have an array arr[0 . . . n-1]. We should be able to efficiently find the minimum value from index L (query start) to R (query end) where 0 &lt;= L &lt;= R &lt;= n-1. Consider a situation when there are many range queries. 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 Minim
15+ min read
Cholesky Decomposition : Matrix Decomposition
In linear algebra, a matrix decomposition or matrix factorization is a factorization of a matrix into a product of matrices. There are many different matrix decompositions. One of them is Cholesky Decomposition. The Cholesky decomposition or Cholesky factorization is a decomposition of a Hermitian, positive-definite matrix into the product of a low
9 min read
Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
Given an integer N, the task is to find the digital root N2 using the digital root of N. Digital Root of a positive integer is calculated by adding the digits of the integer. If the resultant value is a single digit, then that digit is the digital root. If the resultant value contains two or more digits, those digits are summed and the process is r
6 min read
Check if a number is perfect square without finding square root
Check whether a number is a perfect square or not without finding its square root. Examples: Input: n = 36 Output: Yes Input: n = 12 Output: No Recommended PracticeCheck perfect squareTry It! Method 1:The idea is to run a loop from i = 1 to floor(sqrt(n)) and then check if squaring it makes n. Below is the implementation: C/C++ Code // C++ program
9 min read
Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
Given a number ‘n’ and a prime ‘p’, find square root of n under modulo p if it exists. Examples: Input: n = 2, p = 113 Output: 62 62^2 = 3844 and 3844 % 113 = 2 Input: n = 2, p = 7 Output: 3 or 4 3 and 4 both are square roots of 2 under modulo 7 because (3*3) % 7 = 2 and (4*4) % 7 = 2 Input: n = 2, p = 5 Output: Square root doesn't exist We have di
15+ min read
Doolittle Algorithm | LU Decomposition
Doolittle Algorithm: The Doolittle Algorithm is a method for performing LU Decomposition, where a given matrix is decomposed into a lower triangular matrix L and an upper triangular matrix U. This decomposition is widely used in solving systems of linear equations, inverting matrices, and computing determinants. LU DecompositionLet A be a square ma
11 min read
Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
You are given an integer n, find the smallest positive integer root of equation x, or else print -1 if no roots are found.Equation: x^2 + s(x)*x - n = 0where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system. 1 &lt;= N &lt;= 10^18 Examples: Input: N = 110 Output: 10 Explanation: x
8 min read
Find if there is a pair in root to a leaf path with sum equals to root's data
Given a binary tree, find if there is a pair in root to a leaf path such that sum of values in pair is equal to root's data. For example, in below tree there are no pairs in any root to leaf path with sum equal to root's data. The idea is based on hashing and tree traversal. The idea is similar to method 2 of array pair sum problem. Create an empty
8 min read
sqrt, sqrtl and sqrtf in C++
There are various functions available in the C++ Library to calculate the square root of a number. Most prominently, sqrt is used that is defined in &lt;cmath&gt; header file. It takes double as an argument. The &lt;cmath&gt; header defines two more inbuilt functions for calculating the square root of a number (apart from sqrt) which has an argumen
5 min read
Babylonian method for square root
Algorithm: This method can be derived from (but predates) Newton–Raphson method. 1 Start with an arbitrary positive start value x (the closer to the root, the better). 2 Initialize y = 1. 3. Do following until desired approximation is achieved. a) Get the next approximation for root using average of x and y b) Set y = n/x Implementation: C/C++ Code
9 min read
Find square root of number upto given precision using binary search
Given a positive number n and precision p, find the square root of number upto p decimal places using binary search. Note : Prerequisite : Binary search Examples: Input : number = 50, precision = 3 Output : 7.071 Input : number = 10, precision = 4 Output : 3.1622 We have discussed how to compute the integral value of square root in Square Root usin
9 min read
Program to calculate Root Mean Square
Given an array of N numbers. The task is to calculate the Root Mean Square(RMS) of the given numbers. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 3.31662 Input: arr[] = {10, 4, 6, 8} Output: 7.34847 Approach: The Root Mean Square value of N numbers x1,x2,x3,.....xn can be given as, RMS method first calculates the square of each number and then
4 min read
Sum of elements whose square root is present in the array
Given an array arr[], the task is to find the sum of all those elements from the given array whose square root is present in the same array. Examples: Input: arr[] = {1, 2, 3, 4, 6, 9, 10} Output: 13 4 and 9 are the only numbers whose square roots 2 and 3 are present in the array Input: arr[] = {4, 2, 36, 6, 10, 100} Output: 140 Naive Approach: To
10 min read
Range Sum Queries and Update with Square Root
Given an array A of N integers and number of queries Q. You have to answer two types of queries. Update [l, r] – for every i in range from l to r update Ai with sqrt(Ai), where sqrt(Ai) represents the square root of Ai in integral form.Query [l, r] – calculate the sum of all numbers ranging between l and r in array A. Prerequisite: Binary Indexed T
13 min read
Long Division Method to find Square root with Examples
Given an integer X which is a perfect square, the task is to find the square root of it by using the long division method. Examples: Input: N = 484 Output: 22 222 = 484 Input: N = 144 Output: 12 122 = 144 Approach: Long division is a very common method to find the square root of a number. The following is the stepwise solution for this method: 1. D
10 min read
Min operations to reduce N by multiplying by any number or taking square root
Given a number N, the task is to find the minimum value of N by applying below operations any number of times: Multiply N by any positive integerReplace N with sqrt(N), only if N is a perfect square. Examples: Input: N = 20 Output: 10Explanation: Multiply -&gt; 20 * 5 = 100 sqrt(100) = 10, which is the minimum value obtainable. Input: N = 5184 Outp
4 min read
Square root of two Complex Numbers
Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number. Examples: Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 - 0.707107*i Input: A = 4, B = 0Output:The Square roots are: 2-2 Approach: The given problem can
9 min read
Proof: Why the Root Mean Square of two positive numbers is always greater than their Geometric Mean?
This article focuses on discussing the proof of why the root-mean-square of two positive numbers is always greater than their geometric mean. Before getting into the details of the proof, let's first discuss the basic terminologies: Root Mean Square (RMS):Consider some numbers, A1, A2, A3, A4, .... AN, the root-mean-square of these numbers will be
3 min read
Largest integer upto N having greatest prime factor greater than its square root
Given a positive integer N, the task is to find the largest number in the range [1, N] such that the square root of the number is less than its greatest prime factor. Input: N = 15Output: 15Explanation: The prime factors of 15 are {3, 5}. The square root of 15 is 3.87 (i.e, 3.87 &lt; 5). Therefore 15 is the largest valid integer in the given range.
7 min read
Minimum cost to reduce A and B to 0 using square root or divide by 2
Given two integers A and B, the task is to convert the given two integers to zero at minimal cost by performing the following two types of operations: Replace both integers A and B by the square root of the product of A and B. This operation will cost 2 units.Replace A by A/2 or B by B/2 respectively. This operation will cost 1 unit.Example: Input:
15+ min read
Find square root of a number using Bit Manipulation
Given a non-negative integer N, the task is to find the square root of N using bitwise operations. If the integer is not the perfect square, return largest integer that is smaller than or equal to square root of N i.e., floor(√N). Examples: Input: N = 36Output: 6Explanation: The square root of 36 is 6. Input: N = 19Output: 4Explanation: The square
7 min read
Count numbers in range [L, R] that are divisible by square root of a number
Given two numbers L and R, the task is to find the count of all numbers in a given range [L, R] that are divisible by the square root of that number. The square root of a number is the value that, when multiplied by itself, gives the original number. Note: Here square root of a number represents the floor division. i.e, sqrt(4) = 2, sqrt(10) = 3, s
9 min read
Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
Given an integer N and an integer P denoting the product of two primes, the task is to find all possible Square Root of N under modulo P if it exists. It is given that P is the product of p1 and p2, where p1 and p2 are prime numbers of the form 4i + 3 where i is any integer. Some examples of such prime numbers are (7, 11, 19, 23, 31). Examples: Inp
15+ min read
Fast method to calculate inverse square root of a floating point number in IEEE 754 format
Given a 32-bit floating point number x stored in IEEE 754 floating point format, find the inverse square root of x, i.e., x-1/2.A simple solution is to do floating point arithmetic. Following is an example function. C/C++ Code #include &lt;bits/stdc++.h&gt; using namespace std; float InverseSquareRoot(float x) { return 1/sqrt(x); } int main() { cou
4 min read
Euler's criterion (Check if square root under modulo p exists)
Given a number 'n' and a prime p, find if square root of n under modulo p exists or not. A number x is square root of n under modulo p if (x*x)%p = n%p. Examples : Input: n = 2, p = 5 Output: false There doesn't exist a number x such that (x*x)%5 is 2 Input: n = 2, p = 7 Output: true There exists a number x such that (x*x)%7 is 2. The number is 3.
11 min read
three90RightbarBannerImg