# Queries for number of distinct integers in Suffix

Given an array of **N** elements and **Q** queries, where each query contains an integer **K**. For each query, the task is to find the number of distinct integers in the suffix from **K ^{th}** element to

**N**element.

^{th}**Examples:**

Input :N=5, Q=3 arr[] = {2, 4, 6, 10, 2} 1 3 2Output :4 3 4

**Approach:**

The problem can be solved by precomputing the solution for all index from **N-1** to **0**. The idea is to use an unordered_set in C++ as set doesnot allows duplicate elements.

Traverse the array from end and add the current element into a set and the answer for the current index would be the size of the set. So, store the size of set at current index in an auxiliary array say suffixCount.

Below is the implementation of the above approach:

## C++

`// C++ program to find distinct ` `// elements in suffix ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to answer queries for distinct ` `// count in Suffix ` `void` `printQueries(` `int` `n, ` `int` `a[], ` `int` `q, ` `int` `qry[]) ` `{ ` ` ` `// Set to keep the distinct elements ` ` ` `unordered_set<` `int` `> occ; ` ` ` ` ` `int` `suffixCount[n + 1]; ` ` ` ` ` `// Precompute the answer for each suffix ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` `occ.insert(a[i]); ` ` ` `suffixCount[i + 1] = occ.size(); ` ` ` `} ` ` ` ` ` `// Print the precomputed answers ` ` ` `for` `(` `int` `i = 0; i < q; i++) ` ` ` `cout << suffixCount[qry[i]] << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5, q = 3; ` ` ` `int` `a[n] = { 2, 4, 6, 10, 2 }; ` ` ` `int` `qry[q] = { 1, 3, 2 }; ` ` ` ` ` `printQueries(n, a, q, qry); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find distinct

# elements in suffix

# Function to answer queries for distinct

# count in Suffix

def printQueries(n, a, q, qry):

# Set to keep the distinct elements

occ = dict()

suffixCount = [0 for i in range(n + 1)]

# Precompute the answer for each suffix

for i in range(n – 1, -1, -1):

occ[a[i]] = 1

suffixCount[i + 1] = len(occ)

# Pr the precomputed answers

for i in range(q):

print(suffixCount[qry[i]])

# Driver Code

n = 5

q = 3

a = [2, 4, 6, 10, 2]

qry = [1, 3, 2]

printQueries(n, a, q, qry)

# This code is contributed by Mohit Kumar 29

**Output:**

4 3 4

**Time Complexity:** O(N)

**Auxiliary Space**: O(N)

## Recommended Posts:

- Count of distinct substrings of a string using Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- Integers from the range that are composed of a single distinct digit
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Queries on probability of even or odd number in given ranges
- Print next greater number of Q queries
- Queries for number of array elements in a range with Kth Bit Set
- Number of comparisons in each direction for m queries in linear search
- Number of distinct subsets of a set
- Minimum product of k integers in an array of positive Integers
- Find the number of distinct islands in a 2D matrix
- Minimum number of subsets with distinct elements
- Number of unmarked integers in a special sieve
- Count number of distinct pairs whose sum exists in the given array
- Number of strictly increasing Buildings from right with distinct Colors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.