- Related Questions & Answers
- 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++
- Number of Distinct Islands in C++
- Number of Distinct Islands II in C++
- Queries for frequencies of characters in substrings in C++
- Queries to find whether a number has exactly four distinct factors or not in C++
- Queries for counts of multiples in an array in C++
- Convert number string to integers in C++
- Queries on number of Binary sub-matrices of Given size in C++
- Count number of Distinct Substring in a String in C++
- Queries for decimal values of subarrays of a binary array in C++
- Queries for characters in a repeated string in C++
- Median in a stream of integers (running integers) in C++
- Queries on sum of odd number digit sums of all the factors of a number in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −**

array = {2, 6, 1, 2, 7, 6} Q = 2 , queries = {1, 5}

4 2

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.

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,**

#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; }

For Query 1: The number of distinct integer in Suffix is 4 For Query 2: The number of distinct integer in Suffix is 2

Advertisements