
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries for counts of array elements with values in given range in C++
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[6] = 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
#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[0]); int Q = 3; int query[Q][3] = { {1, 4, 8},{2, 6, 5},{1, 1, 4}}; for(int i = 0; i < Q; i++){ if(query[i][0] == 1) cout<<"The count of array elements with value in given range is " <<countElementInRange(arr,N, query[i][1], query[i][2])<<endl; else if(query[i][0] == 2){ cout<<"Updating Value \n"; arr[query[i][1]] = query[i][2]; } } 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
#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[0]); int Q = 3; int query[Q][3] = { {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][0] == 1) cout<<"The count of array elements with value in given range is "<<countElementInRange(arr, n, query[i][1], query[i][2],fenwickTree)<<endl; else if(query[i][0] == 2){ cout<<"Updating Value \n"; UpdateValue(arr, n, query[i][1], query[i][2], 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
- Related Articles
- Queries for counts of multiples in an array in C++
- Array range queries for elements with frequency same as value in C Program?
- Queries for number of array elements in a range with Kth Bit Set using C++
- Construct sum-array with sum of elements in given range in C++
- Queries for bitwise AND in the index range [L, R] of the given Array using C++
- Queries for Bitwise OR in the Index Range [L, R] of the Given Array using C++
- Find the Initial Array from given array after range sum queries in C++
- Min-Max Range Queries in Array in C++
- Python Program for Number of elements with odd factors in the given range
- Queries for decimal values of subarrays of a binary array in C++
- Number of indexes with equal elements in given range in C++
- Check if an array contains all elements of a given range in Python
- Python – Test for all Even elements in the List for the given Range
- Queries to check whether a given digit is present in the given Range in C++
- Return range of values from a masked array along a given axis in NumPy
