- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 integers in Suffix in C++
In this problem, we are given an array of N integers. There are Q queries, each containing an integer value m. Our task to create a program to solve Queries for number of distinct integers in Suffix in C++.
Problem description − Here, we will need to find the total number of distinct integers that are present in the subarray from the index (m-1) to (N-1). Where m is the value in each query.
Let’s take an example to understand the problem −
Input
array = {2, 6, 1, 2, 7, 6} Q = 2 , queries = {1, 5}
Output
4 2
Explanation
For m = 1, we need to find the number of the distinct elements from index 0 to N, which is 4.
For m = 5, we need to find the number of distinct elements from index 4 to N, which is 2.
Solution Approach
The solution to this problem is simple as depicted in the example. We need to simply count the number of unique or distinct elements in the array. If we are considering the data structure as an array, it seems difficult. But if we consider using other data structure the solution will be easy.
So, for solving the problem, we will be using a set in C++ which is available in the STL library. So, for each query, we will insert the elements from the index (m-1) to (n-1) in the set. And the length of the set gives the number of distinct elements for the query.
Program to illustrate our solution,
Example
#include <bits/stdc++.h> using namespace std; int findDistinctInt(int arr[], int N, int m) { set<int> distValues; for (int i = (N-1); i >= (m-1); i--) { distValues.insert(arr[i]); } return distValues.size(); } int main() { int arr[] = { 2, 6, 1, 2, 7, 6 }; int N = sizeof(arr) / sizeof(arr[0]); int Q = 2; int query[Q] = { 1, 5 }; for(int i = 0; i < Q; i++) cout<<"For query "<<(i+1)<<": The number of distinct integer in Suffix is "<<findDistinctInt(arr, N, query[i])<<endl; return 0; }
Output
For Query 1: The number of distinct integer in Suffix is 4 For Query 2: The number of distinct integer in Suffix is 2
- Related Articles
- Queries for number of distinct integers in Suffix in C++ Program
- Queries for number of distinct elements in a subarray in C++
- Queries for number of distinct elements in a subarray | Set 2 in C++
- Queries to find whether a number has exactly four distinct factors or not in C++
- How to apply DISTINCT constraint on select columns in queries in PostgreSQL?
- Number of Distinct Islands in C++
- Why do integers in database row tuple have an 'L' suffix in MySQL?
- Program to find number of different substrings of a string for different queries in Python
- Queries for frequencies of characters in substrings in C++
- Number of Distinct Islands II in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Queries for counts of multiples in an array in C++
- Count the number of distinct values in MySQL?
- Number of integers with odd number of set bits in C++
- C++ Queries for DFS of a Subtree in a Tree
