- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Queries for number of distinct elements in a subarray in C++
In this problem, we are given an array arr[] of size n. And Q queries, each consisting of two elements l and r. Our task is to create a program to solve Queries for number of distinct elements in a subarray in C++.
Problem description − Here for each querry, we need to find the total number of distinct integers in the subarray starting from arr[l] to arr[r].
Let’s take an example to understand the problem,
Input
arr[] = {5, 6, 1, 6, 5, 2, 1} Q = 2 {{1, 4}, {0, 6}}
Output
3 4
Explanation
For querry 1: l = 1 and r = 4, subarray[1...4] = {6, 1, 6, 5}, distinct element’s = 3.
For querry 2 − l = 0 and r = 6, subarray[0...6] = {5, 6, 1, 6, 5, 2, 1}, distinct element’s = 4.
Solution Approach
To solve the problem, we will use the set data structure, whose length will give the count of distinct elements of the array for the range given in the querry. For each querry, we will insert all elements of the range in the array to the set. All duplicate elements of the subarray will be discarded and only distinct elements will be stored, hence the size of the set will give the number of distinct elements.
Progam to illustrate the working of our solution,
Example
#include<bits/stdc++.h> using namespace std; int solveQuery(int arr[], int l, int r) { set<int> distElements; for (int i = (r); i >= (l); i--) distElements.insert(arr[i]); return distElements.size(); } int main() { int arr[] = {5, 6, 1, 6, 5, 2, 1}; int n = sizeof(arr)/sizeof(arr[0]); int Q = 2; int query[Q][2] = {{1, 4}, {0,6}}; for(int i = 0; i < Q; i++) cout<<"For Query "<<(i+1)<<": The number of distinct elements in subarray is "<<solveQuery(arr, query[i][0], query[i][1])<<"\n"; return 0; }
Output
For Query 1: The number of distinct elements in subarray is 3 For Query 2: The number of distinct elements in subarray is 4
- Related Articles
- Queries for number of distinct elements in a subarray | Set 2 in C++
- Queries for number of distinct integers in Suffix in C++
- Queries for number of distinct integers in Suffix in C++ Program
- XOR Queries of a Subarray in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Number of elements less than or equal to a given number in a given subarray in C++
- Queries to find whether a number has exactly four distinct factors or not in C++
- C++ program to find number of distinct values we can get after negating a subarray
- Queries for counts of array elements with values in given range in C++
- Number of Distinct Islands in C++
- C++ Queries for DFS of a Subtree in a Tree
- Maximize number of 0s by flipping a subarray in C++
- Find the Number of Primes In A Subarray using C++
- Array range queries for elements with frequency same as value in C Program?
- Count number of Distinct Substring in a String in C++
