# Queries for counts of array elements with values in given range in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] and Q queries, each can be one of the two types,

• {1, L, R}− For the count of array elements in the range [L, R].

• {2, index, val}− For updating the element at index with val.

Our task is to create a program to solve the Queries for counts of array elements with values in given range in C++.

Let’s take an example to understand the problem,

Input:arr[] = {1, 5, 2, 4, 2, 2, 3, 1, 3}

Q = 3

Query = { {1, 4, 8},

{2, 6, 5},

{1, 1, 4}}

Ouput:3 7

## Explanation

Query 1: count array elements in range [4, 8]. Count = 1

Query 2: update element arr = 5. New array, arr[] = {1, 5, 2, 4, 2, 2, 5, 1, 3}

Query 1: count array elements in range [4, 8]. Count = 7

## Solution Approach

A simple solution to the problem, is by directly traversing the array and find all elements that are in the range i.e. L =< element =< R.

## Example

Live Demo

#include <iostream>
using namespace std;
int countElementInRange(int arr[], int N, int L, int R){
int ValueCount = 0;
for (int i = 0; i < N; i++) {
if (arr[i] >= L && arr[i] <= R) {
ValueCount++;
}
}
return ValueCount;
}
int main() {
int arr[] = {1, 5, 2, 4, 2, 2, 3, 1, 3};
int N = sizeof(arr) / sizeof(arr);
int Q = 3;
int query[Q] = { {1, 4, 8},{2, 6, 5},{1, 1, 4}};
for(int i = 0; i < Q; i++){
if(query[i] == 1)
cout<<"The count of array elements with value in given range is " <<countElementInRange(arr,N, query[i], query[i])<<endl;
else if(query[i] == 2){
cout<<"Updating Value \n";
arr[query[i]] = query[i];
}
}
return 0;
}

## Output

The count of array elements with value in given range is 2
Updating Value
The count of array elements with value in given range is 7

The solution to approach iterate once for every loop. Hence its time complexity is of the order O(Q*N).

A better approach to solving the problem could be using the Binary Indexed Tree or Fenwick Tree data structure. Here, we will store the elements of the array in the tree which will enable us to use the array elements as the index. And we can easily find the count of elements of range using the getSum function which is in-built for the data structure.

ElementCount[L, R] = getSum(R) - getSum(L - 1)

## Example

Live Demo

#include <iostream>
using namespace std;
class BinaryIndTree {
public:
int* BIT;
int N;
BinaryIndTree(int N) {
this->N = N;
BIT = new int[N];
for (int i = 0; i < N; i++) {
BIT[i] = 0;
}
}
void update(int index, int increment) {
while (index < N) {
BIT[index] += increment;
index += (index & -index);
}
}
int calcSum(int index) {
int sum = 0;
while (index > 0) {
sum += BIT[index];
index -= (index & -index);
}
return sum;
}
};
void UpdateValue(int* arr, int n, int index, int val, BinaryIndTree*fenwickTree){
int removedElement = arr[index];
fenwickTree->update(removedElement, -1);
arr[index] = val;
fenwickTree->update(val, 1);
}
int countElementInRange(int* arr, int n, int L, int R, BinaryIndTree*
fenwickTree) {
return fenwickTree->calcSum(R) - fenwickTree->calcSum(L - 1);
}
int main() {
int arr[] = { 1, 5, 2, 4, 2, 2, 3, 1, 3 };
int n = sizeof(arr) / sizeof(arr);
int Q = 3;
int query[Q] = { {1, 4, 8},{2, 6, 5},{1, 1, 4}};
int N = 100001;
BinaryIndTree* fenwickTree = new BinaryIndTree(N);
for (int i = 0; i < n; i++)
fenwickTree->update(arr[i], 1);
for(int i = 0; i < Q; i++){
if(query[i] == 1)
cout<<"The count of array elements with value in given range is "<<countElementInRange(arr, n, query[i], query[i],fenwickTree)<<endl;
else if(query[i] == 2){
cout<<"Updating Value \n";
UpdateValue(arr, n, query[i], query[i], fenwickTree);
}
}
return 0;
}

## Output

The count of array elements with value in given range is 2
Updating Value
The count of array elements with value in given range is 7