Open In App

Binary Indexed Tree : Range Updates and Point Queries

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

Given an array arr[0..n-1]. The following operations need to be performed.

  1. update(l, r, val): Add ‘val’ to all the elements in the array from [l, r].
  2. getElement(i): Find element in the array indexed at ‘i’.

Initially all the elements in the array are 0. Queries can be in any order, i.e., there can be many updates before point query.

Example:

Input: arr = {0, 0, 0, 0, 0}
Queries: update : l = 0, r = 4, val = 2
         getElement : i = 3
         update : l = 3, r = 4, val = 3 
         getElement : i = 3

Output: Element at 3 is 2
        Element at 3 is 5
 
Explanation: Array after first update becomes
              {2, 2, 2, 2, 2}
              Array after second update becomes
              {2, 2, 2, 5, 5}

Method 1 [update : O(n), getElement() : O(1)]

  1. update(l, r, val): Iterate over the subarray from l to r and increase all the elements by val.
  2. getElement(i): To get the element at i’th index, simply return arr[i].

The time complexity in the worst case is O(q*n) where q is the number of queries and n is the number of elements.  

Method 2 [update: O(1), getElement(): O(n)]

We can avoid updating all elements and can update only 2 indexes of the array!

  1. update(l, r, val) : Add ‘val’ to the lth element and subtract ‘val’ from the (r+1)th element, do this for all the update queries.
  arr[l]   = arr[l] + val
  arr[r+1] = arr[r+1] - val
  1. getElement(i) : To get ith element in the array find the sum of all integers in the array from 0 to i.(Prefix Sum).

Let’s analyze the update query. Why to add val to lth index? Adding val to lth index means that all the elements after l are increased by val, since we will be computing the prefix sum for every element. Why to subtract val from (r+1)th index? A range update was required from [l,r] but what we have updated is [l, n-1] so we need to remove val from all the elements after r i.e., subtract val from (r+1)th index. Thus the val is added to range [l,r]. Below is the implementation of the above approach. 

C++




// C++ program to demonstrate Range Update
// and Point Queries Without using BIT
#include <bits/stdc++.h>
using namespace std;
 
// Updates such that getElement() gets an increased
// value when queried from l to r.
void update(int arr[], int l, int r, int val)
{
    arr[l] += val;
    arr[r+1] -= val;
}
 
// Get the element indexed at i
int getElement(int arr[], int i)
{
    // To get ith element sum of all the elements
    // from 0 to i need to be computed
    int res = 0;
    for (int j = 0 ; j <= i; j++)
        res += arr[j];
 
    return res;
}
 
// Driver program to test above function
int main()
{
    int arr[] = {0, 0, 0, 0, 0};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int l = 2, r = 4, val = 2;
    update(arr, l, r, val);
 
    //Find the element at Index 4
    int index = 4;
    cout << "Element at index " << index << " is " <<
         getElement(arr, index) << endl;
 
    l = 0, r = 3, val = 4;
    update(arr,l,r,val);
 
    //Find the element at Index 3
    index = 3;
    cout << "Element at index " << index << " is " <<
         getElement(arr, index) << endl;
 
    return 0;
}


Java




// Java program to demonstrate Range Update
// and Point Queries Without using BIT
class GfG {
 
// Updates such that getElement() gets an increased
// value when queried from l to r.
static void update(int arr[], int l, int r, int val)
{
    arr[l] += val;
    if(r + 1 < arr.length)
    arr[r+1] -= val;
}
 
// Get the element indexed at i
static int getElement(int arr[], int i)
{
    // To get ith element sum of all the elements
    // from 0 to i need to be computed
    int res = 0;
    for (int j = 0 ; j <= i; j++)
        res += arr[j];
 
    return res;
}
 
// Driver program to test above function
public static void main(String[] args)
{
    int arr[] = {0, 0, 0, 0, 0};
    int n = arr.length;
 
    int l = 2, r = 4, val = 2;
    update(arr, l, r, val);
 
    //Find the element at Index 4
    int index = 4;
    System.out.println("Element at index " + index + " is " +getElement(arr, index));
 
    l = 0;
    r = 3;
    val = 4;
    update(arr,l,r,val);
 
    //Find the element at Index 3
    index = 3;
    System.out.println("Element at index " + index + " is " +getElement(arr, index));
 
}
}


Python3




# Python3 program to demonstrate Range
# Update and PoQueries Without using BIT
 
# Updates such that getElement() gets an
# increased value when queried from l to r.
def update(arr, l, r, val):
    arr[l] += val
    if r + 1 < len(arr):
        arr[r + 1] -= val
 
# Get the element indexed at i
def getElement(arr, i):
     
    # To get ith element sum of all the elements
    # from 0 to i need to be computed
    res = 0
    for j in range(i + 1):
        res += arr[j]
 
    return res
 
# Driver Code
if __name__ == '__main__':
    arr = [0, 0, 0, 0, 0]
    n = len(arr)
 
    l = 2
    r = 4
    val = 2
    update(arr, l, r, val)
 
    # Find the element at Index 4
    index = 4
    print("Element at index", index,
          "is", getElement(arr, index))
 
    l = 0
    r = 3
    val = 4
    update(arr, l, r, val)
 
    # Find the element at Index 3
    index = 3
    print("Element at index", index,
          "is", getElement(arr, index))
 
# This code is contributed by PranchalK


C#




// C# program to demonstrate Range Update
// and Point Queries Without using BIT
using System;
 
class GfG
{
 
// Updates such that getElement()
// gets an increased value when
// queried from l to r.
static void update(int []arr, int l,
                    int r, int val)
{
    arr[l] += val;
    if(r + 1 < arr.Length)
    arr[r + 1] -= val;
}
 
// Get the element indexed at i
static int getElement(int []arr, int i)
{
    // To get ith element sum of all the elements
    // from 0 to i need to be computed
    int res = 0;
    for (int j = 0 ; j <= i; j++)
        res += arr[j];
 
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = {0, 0, 0, 0, 0};
    int n = arr.Length;
 
    int l = 2, r = 4, val = 2;
    update(arr, l, r, val);
 
    //Find the element at Index 4
    int index = 4;
    Console.WriteLine("Element at index " +
                        index + " is " +
                        getElement(arr, index));
 
    l = 0;
    r = 3;
    val = 4;
    update(arr,l,r,val);
 
    //Find the element at Index 3
    index = 3;
    Console.WriteLine("Element at index " +
                            index + " is " +
                            getElement(arr, index));
}
}
 
// This code is contributed by PrinciRaj1992


PHP




<?php
// PHP program to demonstrate Range Update
// and Point Queries Without using BIT
 
// Updates such that getElement() gets an
// increased value when queried from l to r.
function update(&$arr, $l, $r, $val)
{
    $arr[$l] += $val;
    if($r + 1 < sizeof($arr))
    $arr[$r + 1] -= $val;
}
 
// Get the element indexed at i
function getElement(&$arr, $i)
{
    // To get ith element sum of all the elements
    // from 0 to i need to be computed
    $res = 0;
    for ($j = 0 ; $j <= $i; $j++)
        $res += $arr[$j];
 
    return $res;
}
 
// Driver Code
$arr = array(0, 0, 0, 0, 0);
$n = sizeof($arr);
 
$l = 2; $r = 4; $val = 2;
update($arr, $l, $r, $val);
 
// Find the element at Index 4
$index = 4;
echo("Element at index " . $index .
     " is " . getElement($arr, $index) . "\n");
 
$l = 0;
$r = 3;
$val = 4;
update($arr, $l, $r, $val);
 
// Find the element at Index 3
$index = 3;
echo("Element at index " . $index .
     " is " . getElement($arr, $index));
 
// This code is contributed by Code_Mech
?>


Javascript




//JavaScript program to demonstrate Range Update
// and Point Queries Without using BIT
 
// Updates such that getElement() gets an increased
// value when queried from l to r.
function update(arr, l, r, val)
{
    arr[l] += val;
    arr[r+1] -= val;
}
 
// Get the element indexed at i
function getElement(rr, i)
{
 
    // To get ith element sum of all the elements
    // from 0 to i need to be computed
    let res = 0;
    for (let j = 0 ; j <= i; j++)
        res += arr[j];
 
    return res;
}
 
// Driver program to test above function
 
    let arr = [0, 0, 0, 0, 0];
    let n = arr.length;
 
    let l = 2, r = 4, val = 2;
    update(arr, l, r, val);
 
    // Find the element at Index 4
    let index = 4;
    console.log("Element at index ",index," is ",getElement(arr, index));
 
    l = 0, r = 3, val = 4;
    update(arr,l,r,val);
 
    // Find the element at Index 3
    index = 3;
    console.log("Element at index ",index," is ",getElement(arr, index));
 
// This code is contributed by vikkycirus


Output:

Element at index 4 is 2
Element at index 3 is 6

Time complexity : O(q*n) where q is number of queries.  

Auxiliary Space: O(n)

Method 3 (Using Binary Indexed Tree)

In method 2, we have seen that the problem can reduced to update and prefix sum queries. We have seen that BIT can be used to do update and prefix sum queries in O(Logn) time. Below is the implementation. 

C++




// C++ code to demonstrate Range Update and
// Point Queries on a Binary Index Tree
#include <bits/stdc++.h>
using namespace std;
 
// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void updateBIT(int BITree[], int n, int index, int val)
{
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;
 
    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
        // Add 'val' to current node of BI Tree
        BITree[index] += val;
 
        // Update index to that of parent in update View
        index += index & (-index);
    }
}
 
// Constructs and returns a Binary Indexed Tree for given
// array of size n.
int *constructBITree(int arr[], int n)
{
    // Create and initialize BITree[] as 0
    int *BITree = new int[n+1];
    for (int i=1; i<=n; i++)
        BITree[i] = 0;
 
    // Store the actual values in BITree[] using update()
    for (int i=0; i<n; i++)
        updateBIT(BITree, n, i, arr[i]);
 
    // Uncomment below lines to see contents of BITree[]
    //for (int i=1; i<=n; i++)
    //      cout << BITree[i] << " ";
 
    return BITree;
}
 
// SERVES THE PURPOSE OF getElement()
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[]
int getSum(int BITree[], int index)
{
    int sum = 0; // Initialize result
 
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;
 
    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += BITree[index];
 
        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}
 
// Updates such that getElement() gets an increased
// value when queried from l to r.
void update(int BITree[], int l, int r, int n, int val)
{
    // Increase value at 'l' by 'val'
    updateBIT(BITree, n, l, val);
 
    // Decrease value at 'r+1' by 'val'
    updateBIT(BITree, n, r+1, -val);
}
 
// Driver program to test above function
int main()
{
    int arr[] = {0, 0, 0, 0, 0};
    int n = sizeof(arr)/sizeof(arr[0]);
    int *BITree = constructBITree(arr, n);
 
    // Add 2 to all the element from [2,4]
    int l = 2, r = 4, val = 2;
    update(BITree, l, r, n, val);
 
    // Find the element at Index 4
    int index = 4;
    cout << "Element at index " << index << " is " <<
         getSum(BITree,index) << "\n";
 
    // Add 2 to all the element from [0,3]
    l = 0, r = 3, val = 4;
    update(BITree, l, r, n, val);
 
    // Find the element at Index 3
    index = 3;
    cout << "Element at index " << index << " is " <<
         getSum(BITree,index) << "\n" ;
 
    return 0;
}


Java




/* Java code to demonstrate Range Update and
* Point Queries on a Binary Index Tree.
* This method only works when all array
* values are initially 0.*/
class GFG
{
 
    // Max tree size
    final static int MAX = 1000;
 
    static int BITree[] = new int[MAX];
 
    // Updates a node in Binary Index
    // Tree (BITree) at given index
    // in BITree. The given value 'val'
    // is added to BITree[i] and
    // all of its ancestors in tree.
    public static void updateBIT(int n,
                                 int index,
                                 int val)
    {
        // index in BITree[] is 1
        // more than the index in arr[]
        index = index + 1;
 
        // Traverse all ancestors
        // and add 'val'
        while (index <= n)
        {
            // Add 'val' to current
            // node of BITree
            BITree[index] += val;
 
            // Update index to that
            // of parent in update View
            index += index & (-index);
        }
    }
 
    // Constructs Binary Indexed Tree
    // for given array of size n.
 
    public static void constructBITree(int arr[],
                                       int n)
    {
        // Initialize BITree[] as 0
        for(int i = 1; i <= n; i++)
            BITree[i] = 0;
 
        // Store the actual values
        // in BITree[] using update()
        for(int i = 0; i < n; i++)
            updateBIT(n, i, arr[i]);
 
        // Uncomment below lines to
        // see contents of BITree[]
        // for (int i=1; i<=n; i++)
        //     cout << BITree[i] << " ";
    }
 
    // SERVES THE PURPOSE OF getElement()
    // Returns sum of arr[0..index]. This
    // function assumes that the array is
    // preprocessed and partial sums of
    // array elements are stored in BITree[]
    public static int getSum(int index)
    {
        int sum = 0; //Initialize result
 
        // index in BITree[] is 1 more
        // than the index in arr[]
        index = index + 1;
 
        // Traverse ancestors
        // of BITree[index]
        while (index > 0)
        {
 
            // Add current element
            // of BITree to sum
            sum += BITree[index];
 
            // Move index to parent
            // node in getSum View
            index -= index & (-index);
        }
 
        // Return the sum
        return sum;
    }
 
    // Updates such that getElement()
    // gets an increased value when
    // queried from l to r.
    public static void update(int l, int r,
                              int n, int val)
    {
        // Increase value at
        // 'l' by 'val'
        updateBIT(n, l, val);
 
        // Decrease value at
        // 'r+1' by 'val'
        updateBIT(n, r + 1, -val);
    }
 
 
    // Driver Code
    public static void main(String args[])
    {
        int arr[] = {0, 0, 0, 0, 0};
        int n = arr.length;
 
        constructBITree(arr,n);
 
        // Add 2 to all the
        // element from [2,4]
        int l = 2, r = 4, val = 2;
        update(l, r, n, val);
 
        int index = 4;
 
        System.out.println("Element at index "+
                                index + " is "+
                                getSum(index));
 
        // Add 2 to all the
        // element from [0,3]
        l = 0; r = 3; val = 4;
        update(l, r, n, val);
 
        // Find the element
        // at Index 3
        index = 3;
        System.out.println("Element at index "+
                                index + " is "+
                                getSum(index));
    }
}
// This code is contributed
// by Puneet Kumar.


Python3




# Python3 code to demonstrate Range Update and
# PoQueries on a Binary Index Tree
 
# Updates a node in Binary Index Tree (BITree) at given index
# in BITree. The given value 'val' is added to BITree[i] and
# all of its ancestors in tree.
def updateBIT(BITree, n, index, val):
     
    # index in BITree[] is 1 more than the index in arr[]
    index = index + 1
 
    # Traverse all ancestors and add 'val'
    while (index <= n):
         
        # Add 'val' to current node of BI Tree
        BITree[index] += val
 
        # Update index to that of parent in update View
        index += index & (-index)
 
# Constructs and returns a Binary Indexed Tree for given
# array of size n.
def constructBITree(arr, n):
     
    # Create and initialize BITree[] as 0
    BITree = [0]*(n+1)
 
    # Store the actual values in BITree[] using update()
    for i in range(n):
        updateBIT(BITree, n, i, arr[i])
 
    return BITree
 
# SERVES THE PURPOSE OF getElement()
# Returns sum of arr[0..index]. This function assumes
# that the array is preprocessed and partial sums of
# array elements are stored in BITree[]
def getSum(BITree, index):
    sum = 0 # Initialize result
 
    # index in BITree[] is 1 more than the index in arr[]
    index = index + 1
 
    # Traverse ancestors of BITree[index]
    while (index > 0):
         
        # Add current element of BITree to sum
        sum += BITree[index]
 
        # Move index to parent node in getSum View
        index -= index & (-index)
    return sum
 
# Updates such that getElement() gets an increased
# value when queried from l to r.
def update(BITree, l, r, n, val):
     
    # Increase value at 'l' by 'val'
    updateBIT(BITree, n, l, val)
 
    # Decrease value at 'r+1' by 'val'
    updateBIT(BITree, n, r+1, -val)
 
# Driver code
arr = [0, 0, 0, 0, 0]
n = len(arr)
BITree = constructBITree(arr, n)
 
# Add 2 to all the element from [2,4]
l = 2
r = 4
val = 2
update(BITree, l, r, n, val)
 
# Find the element at Index 4
index = 4
print("Element at index", index, "is", getSum(BITree, index))
 
# Add 2 to all the element from [0,3]
l = 0
r = 3
val = 4
update(BITree, l, r, n, val)
 
# Find the element at Index 3
index = 3
print("Element at index", index, "is", getSum(BITree,index))
 
# This code is contributed by mohit kumar 29


C#




using System;
 
/* C# code to demonstrate Range Update and
* Point Queries on a Binary Index Tree.
* This method only works when all array
* values are initially 0.*/
public class GFG
{
 
    // Max tree size
    public const int MAX = 1000;
 
    public static int[] BITree = new int[MAX];
 
    // Updates a node in Binary Index
    // Tree (BITree) at given index
    // in BITree. The given value 'val'
    // is added to BITree[i] and
    // all of its ancestors in tree.
    public static void updateBIT(int n, int index, int val)
    {
        // index in BITree[] is 1 
        // more than the index in arr[]
        index = index + 1;
 
        // Traverse all ancestors 
        // and add 'val'
        while (index <= n)
        {
            // Add 'val' to current 
            // node of BITree
            BITree[index] += val;
 
            // Update index to that 
            // of parent in update View
            index += index & (-index);
        }
    }
 
    // Constructs Binary Indexed Tree 
    // for given array of size n.
 
    public static void constructBITree(int[] arr, int n)
    {
        // Initialize BITree[] as 0
        for (int i = 1; i <= n; i++)
        {
            BITree[i] = 0;
        }
 
        // Store the actual values 
        // in BITree[] using update()
        for (int i = 0; i < n; i++)
        {
            updateBIT(n, i, arr[i]);
        }
 
        // Uncomment below lines to 
        // see contents of BITree[]
        // for (int i=1; i<=n; i++)
        //     cout << BITree[i] << " ";
    }
 
    // SERVES THE PURPOSE OF getElement()
    // Returns sum of arr[0..index]. This 
    // function assumes that the array is
    // preprocessed and partial sums of
    // array elements are stored in BITree[]
    public static int getSum(int index)
    {
        int sum = 0; //Initialize result
 
        // index in BITree[] is 1 more 
        // than the index in arr[]
        index = index + 1;
 
        // Traverse ancestors
        // of BITree[index]
        while (index > 0)
        {
 
            // Add current element 
            // of BITree to sum
            sum += BITree[index];
 
            // Move index to parent 
            // node in getSum View
            index -= index & (-index);
        }
 
        // Return the sum
        return sum;
    }
 
    // Updates such that getElement() 
    // gets an increased value when 
    // queried from l to r.
    public static void update(int l, int r, int n, int val)
    {
        // Increase value at 
        // 'l' by 'val'
        updateBIT(n, l, val);
 
        // Decrease value at
        // 'r+1' by 'val'
        updateBIT(n, r + 1, -val);
    }
 
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = new int[] {0, 0, 0, 0, 0};
        int n = arr.Length;
 
        constructBITree(arr,n);
 
        // Add 2 to all the
        // element from [2,4]
        int l = 2, r = 4, val = 2;
        update(l, r, n, val);
 
        int index = 4;
 
        Console.WriteLine("Element at index " + index + " is " + getSum(index));
 
        // Add 2 to all the 
        // element from [0,3]
        l = 0;
        r = 3;
        val = 4;
        update(l, r, n, val);
 
        // Find the element
        // at Index 3
        index = 3;
        Console.WriteLine("Element at index " + index + " is " + getSum(index));
    }
}
 
 
  // This code is contributed by Shrikant13


Javascript




// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
function updateBIT(BITree, n, index, val) {
  // index in BITree[] is 1 more than the index in arr[]
  index = index + 1;
 
  // Traverse all ancestors and add 'val'
  while (index <= n) {
    // Add 'val' to current node of BI Tree
    BITree[index] += val;
 
    // Update index to that of parent in update View
    index += index & (-index);
  }
}
 
// Constructs and returns a Binary Indexed Tree for given
// array of size n.
function constructBITree(arr, n) {
  // Create and initialize BITree[] as 0
  let BITree = new Array(n+1).fill(0);
 
  // Store the actual values in BITree[] using update()
  for (let i = 0; i < n; i++) {
    updateBIT(BITree, n, i, arr[i]);
  }
 
  return BITree;
}
 
// SERVES THE PURPOSE OF getElement()
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[]
function getSum(BITree, index) {
  let sum = 0; // Initialize result
 
  // index in BITree[] is 1 more than the index in arr[]
  index = index + 1;
 
  // Traverse ancestors of BITree[index]
  while (index > 0) {
    // Add current element of BITree to sum
    sum += BITree[index];
 
    // Move index to parent node in getSum View
    index -= index & (-index);
  }
  return sum;
}
 
// Updates such that getElement() gets an increased
// value when queried from l to r.
function update(BITree, l, r, n, val) {
  // Increase value at 'l' by 'val'
  updateBIT(BITree, n, l, val);
 
  // Decrease value at 'r+1' by 'val'
  updateBIT(BITree, n, r+1, -val);
}
 
// Test the functions
 
let arr = [0, 0, 0, 0, 0];
let n = arr.length;
let BITree = constructBITree(arr, n);
 
// Add 2 to all the element from [2,4]
let l = 2, r = 4, val = 2;
update(BITree, l, r, n, val);
 
// Find the element at Index 4
let index = 4;
console.log(`Element at index ${index} is ${getSum(BITree,index)}`);
 
// Add 2 to all the element from [0,3]
l = 0, r = 3, val = 4;
update(BITree, l, r, n, val);
 
// Find the element at Index 3
index = 3;
console.log(`Element at index ${index} is ${getSum(BITree,index)}`);


Output:

Element at index 4 is 2
Element at index 3 is 6

Time Complexity : O(q * log n) + O(n * log n) where q is number of queries. 

Auxiliary Space: O(n)

Method 1 is efficient when most of the queries are getElement(), method 2 is efficient when most of the queries are updates() and method 3 is preferred when there is mix of both queries.



Similar Reads

Binary Indexed Tree : Range Update and Range Queries
Given an array arr[0..N-1]. The following operations need to be performed. update(l, r, val): Add ‘val’ to all the elements in the array from [l, r].getRangeSum(l, r): Find the sum of all elements in the array from [l, r].Initially, all the elements in the array are 0. Queries can be in any order, i.e., there can be many updates before range sum. E
15+ min read
Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
Prerequisites: Segment TreeGiven a number N which represents the size of the array initialized to 0 and Q queries to process where there are two types of queries: 1 P V: Put the value V at position P.2 L R: Output the sum of values from L to R. The task is to answer these queries. Constraints: 1 ? N ? 1018Q ? 1051 ? L ? R? N Note: Queries are onlin
15+ min read
Queries for Composite numbers in subarray (With Point Updates)
Given an array of N integers, the task is to perform the following two operations on the given array: query(start, end) : Print the number of Composite numbers in the subarray from start to end update(i, x) : update the value at index i to x, i.e arr[i] = x Examples: Input : arr = {1, 12, 3, 5, 17, 9} Query 1: query(start = 0, end = 4) Query 2: upd
15+ min read
Array range queries to count the number of Fibonacci numbers with updates
Given an array arr[] of N integers, the task is to perform the following two queries: query(start, end): Print the number of fibonacci numbers in the subarray from start to endupdate(i, x): Add x to the array element referenced by array index i, that is: arr[i] = x Examples: Input: arr = { 1, 2, 3, 4, 8, 9 } Query 1: query(start = 0, end = 4) Query
15+ min read
Array Range Queries to count Powerful numbers with updates
Given an array of N integers, the task is to perform the following two operations on the given array: query(L, R): Print the number of Powerful numbers in the subarray from L to R. update(i, x) : update the value at index i to x, i.e arr[i] = x A number N is said to be Powerful Number if, for every prime factor p of it, p2 also divides it.Prerequis
15+ min read
Range Queries to count the number of even parity values with updates
Given an array arr[] of N integers, the task is to perform the following two queries: query(L, R): Print the number of Even Parity numbers in the subarray from L to R.update(i, x): Update the array element reference by index i to x. Examples: Input: arr[] = {18, 15, 8, 9, 14, 5} Query 1: query(L = 0, R = 4) Query 2: update(i = 3, x = 11) Query 3: q
15+ min read
Array range queries to find the number of perfect square elements with updates
Given an array arr[] of N integers, the task is to perform the following two queries: query(start, end): Print the number of perfect square numbers in the sub-array from start to endupdate(i, x): Add x to the array element referenced by array index i, that is: arr[i] = x Note: 0 based indexing is followed in the below example. Example: Input: arr =
15+ min read
Range Queries for number of Armstrong numbers in an array with updates
Given an array arr[] of N integers, the task is to perform the following two queries: query(start, end): Print the number of Armstrong numbers in the subarray from start to endupdate(i, x): Add x to the array element referenced by array index i, that is: arr[i] = x Examples: Input: arr = { 18, 153, 8, 9, 14, 5} Query 1: query(start = 0, end = 4) Qu
15+ min read
Queries for count of array elements with values in given range with updates
Given an array arr[] of size N and a matrix Q consisting of queries of the following two types: 1 L R : Print the number of elements lying in the range [L, R].2 i x : Set arr[i] = x Examples: Input: arr[] = {1, 2, 2, 3, 4, 4, 5, 6}, Q = {{1, {3, 5}}, {1, {2, 4}}, {1, {1, 2}}, {2, {1, 7}}, {1, {1, 2}}} Output: 4 5 3 2 Explanation: Array elements fro
15+ min read
Count of Ks in the Array for a given range of indices after array updates for Q queries
Given an array arr[] of N integers, an integer K, and Q queries of the type as explained below: (1, L, R): If the query is of type 1, then find the number of Ks over the range [L, R].(2, P, X): If the query is of type 2, then update the array element at index P to X. The task is to perform the queries on the given Array and print the result accordi
15+ min read
Article Tags :
Practice Tags :