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 from the range [3, 5] are {3, 4, 4, 5}
Array elements from the range [2, 4] are {2, 2, 3, 4, 4}
Array elements from the range [1, 2] are {1, 2, 2}
Replacing arr[1] by 7 modifies the array to {1, 7, 2, 3, 4, 4, 5, 6}
Elements that lie in range [1, 2] are {1, 2}Input: arr = {5, 5, 1, 3, 4, 4, 2, 3}, Q = {{1, {3, 6}}, {1, {2, 4}}, {1, {10, 20}}}
Output: 6 5 0
Explanation:
Array elements from the range [3, 6] are {3, 3, 4, 4, 5, 5}
Array elements from the range [2, 4] are {2, 3, 3, 4, 4}
No element from the range [10, 20] exists in the array.
Naive Approach:
The simplest approach to solve this problem is as follows:
- For the query of type (1 L R), iterate over the entire array and count the number of elements in the array such that L ≤ arr[i] ≤ R. Finally, print the count.
- For the query of type (2 i x), replace arr[i] by x.
Below is the implementation of the above approach:
C++
// C++ code for queries for number // of elements that lie in range // [l, r] (with updates) #include <bits/stdc++.h> using namespace std; // Function to set arr[index] = x void setElement(int* arr, int n, int index, int x) { arr[index] = x; } // Function to get count of elements // that lie in range [l, r] int getCount(int* arr, int n, int l, int r) { int count = 0; // Traverse array for (int i = 0; i < n; i++) { // If element lies in the // range [L, R] if (arr[i] >= l && arr[i] <= r) { // Increase count count++; } } return count; } // Function to solve each query void SolveQuery(int arr[], int n, vector<pair<int, pair<int, int> > > Q) { int x; for (int i = 0; i < Q.size(); i++) { if (Q[i].first == 1) { x = getCount(arr, n, Q[i].second.first, Q[i].second.second); cout << x << " "; } else { setElement(arr, n, Q[i].second.first, Q[i].second.second); } } } // Driver Code int main() { int arr[] = { 1, 2, 2, 3, 4, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); vector<pair<int, pair<int, int> > > Q = { { 1, { 3, 5 } }, { 1, { 2, 4 } }, { 1, { 1, 2 } }, { 2, { 1, 7 } }, { 1, { 1, 2 } } }; SolveQuery(arr, n, Q); return 0; } |
4 5 3 2
Time Complexity: O(Q * N)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized using Fenwick Tree. Follow the steps below to solve the problem:
- Construct a Fenwick tree from the given array.
- Fenwick tree will be represented as an array of size equal to maximum element in the array, so that the array elements can be used as index (idea is similar to this approach).
- Traverse the array and increase the frequency of every element by calling the update method of Fenwick tree.
-
For each query of type (1 L R), call the getSum method of Fenwick tree. The answer for the query of type 1 will be:
getSum(R) – getSum(L – 1)
- For each query of type (2 i x), call the update method of Fenwick tree to increase the frequency of the added element and decrease the count of the element to be replaced.
Below is the implementation of the above approach:
C++
// C++ code for queries for number // of elements that lie in range // [l, r] (with updates) #include <bits/stdc++.h> using namespace std; class FenwickTree { public: int* BIT; int N; FenwickTree(int N) { this->N = N; BIT = new int[N]; for (int i = 0; i < N; i++) { BIT[i] = 0; } } // Traverse all ancestors and // increase frequency of index void update(int index, int increment) { while (index < N) { // Increase count of the current // node of BIT Tree BIT[index] += increment; // Update index to that of parent // in update View index += (index & -index); } } // Function to return the // sum of arr[0..index] int getSum(int index) { int sum = 0; // Traverse ancestors of // BITree[index] while (index > 0) { // Add current element of // BITree to sum sum += BIT[index]; // Move index to parent node in // getSum View index -= (index & -index); } return sum; } }; // Function to set arr[index] = x void setElement(int* arr, int n, int index, int x, FenwickTree* fenTree) { int removedElement = arr[index]; fenTree->update(removedElement, -1); arr[index] = x; fenTree->update(x, 1); } // Function to get count of // elements that lie in // range [l, r] int getCount(int* arr, int n, int l, int r, FenwickTree* fenTree) { int count = fenTree->getSum(r) - fenTree->getSum(l - 1); return count; } // Function to solve each query void SolveQuery(int arr[], int n, vector<pair<int, pair<int, int> > > Q) { int N = 100001; FenwickTree* fenTree = new FenwickTree(N); for (int i = 0; i < n; i++) { fenTree->update(arr[i], 1); } int x; for (int i = 0; i < Q.size(); i++) { if (Q[i].first == 1) { x = getCount(arr, n, Q[i].second.first, Q[i].second.second, fenTree); cout << x << " "; } else { setElement(arr, n, Q[i].second.first, Q[i].second.second, fenTree); } } } // Driver Code int main() { int arr[] = { 1, 2, 2, 3, 4, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); vector<pair<int, pair<int, int> > > Q = { { 1, { 3, 5 } }, { 1, { 2, 4 } }, { 1, { 1, 2 } }, { 2, { 1, 7 } }, { 1, { 1, 2 } } }; SolveQuery(arr, n, Q); return 0; } |
4 5 3 2
Time Complexity: O(n*log(N) + Q*log(N))
Auxiliary Space: O(maxm), where maxm is the maximum element present in the array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Range Queries to count the number of even parity values with updates
- Array Range Queries to count Powerful numbers with updates
- Array range queries to count the number of Fibonacci numbers with updates
- Array range queries to find the number of perfect square elements with updates
- Queries for counts of array elements with values in given range
- String Range Queries to count number of distinct characters with updates
- Range Queries for number of Armstrong numbers in an array with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Array range queries to find the maximum Fibonacci number with updates
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Queries for elements having values within the range A to B using MO's Algorithm
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Range sum queries without updates
- Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed
- Queries to find maximum product pair in range with updates
- Binary Indexed Tree : Range Updates and Point Queries
- Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Count of elements which are power of 2 in a given range subarray for Q queries
- Queries for the count of even digit sum elements in the given range using Segment Tree.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

