# 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 does not 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find distinct ` `// elements in suffix ` ` ` `// Function to answer queries for distinct ` `// count in Suffix ` `function` `printQueries(` `$n` `, ` `$a` `, ` `$q` `, ` `$qry` `) ` `{ ` ` ` `// Set to keep the distinct elements ` ` ` `$occ` `= ` `array` `(); ` ` ` ` ` `$suffixCount` `= ` `array_fill` `(0, ` `$n` `+ 1, 0); ` ` ` ` ` `// Precompute the answer for each suffix ` ` ` `for` `(` `$i` `= ` `$n` `- 1; ` `$i` `>= 0; ` `$i` `--) ` ` ` `{ ` ` ` `array_push` `(` `$occ` `, ` `$a` `[` `$i` `]); ` ` ` ` ` `# give ` `array` `of distinct element ` ` ` `$occ` `= ` `array_unique` `(` `$occ` `); ` ` ` ` ` `$suffixCount` `[` `$i` `+ 1] = sizeof(` `$occ` `); ` ` ` `} ` ` ` ` ` ` ` `// Print the precomputed answers ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$q` `; ` `$i` `++) ` ` ` `echo` `$suffixCount` `[` `$qry` `[` `$i` `]], ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$q` `= 3; ` `$a` `= ` `array` `(2, 4, 6, 10, 2); ` `$qry` `= ` `array` `(1, 3, 2); ` ` ` `printQueries(` `$n` `, ` `$a` `, ` `$q` `, ` `$qry` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4 3 4

**Time Complexity:** O(N)

**Auxiliary Space**: O(N)

**Approach without STL:** Since queries need to be addressed, precomputation needs to be done. Otherwise, a simple traversal over the suffix would have sufficed.

An auxiliary array **aux[]** is maintained from the right side of the array. An array **mark[]** stores whether an element has occurred or not in the suffix traversed yet. If the element has not occurred, then update **aux[i] = aux[i+1] + 1**. Otherwise **aux[i] = aux[i+1]**.

Answer for every query would be **aux[q[i]]**.

Below is the implementation of the above approach.

`// C++ implementation of the above approach. ` `#include <bits/stdc++.h> ` ` ` `#define MAX 100002 ` `using` `namespace` `std; ` ` ` `// Function to print no of unique elements ` `// in specified suffix for each query. ` `void` `printUniqueElementsInSuffix(` `const` `int` `* arr, ` ` ` `int` `n, ` `const` `int` `* q, ` `int` `m) ` `{ ` ` ` ` ` `// aux[i] stores no of unique elements in arr[i..n] ` ` ` `int` `aux[MAX]; ` ` ` ` ` `// mark[i] = 1 if i has occurred in the suffix at least once. ` ` ` `int` `mark[MAX]; ` ` ` `memset` `(mark, 0, ` `sizeof` `(mark)); ` ` ` ` ` `// Initialization for the last element. ` ` ` `aux[n - 1] = 1; ` ` ` `mark[arr[n - 1]] = 1; ` ` ` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) { ` ` ` `if` `(mark[arr[i]] == 0) { ` ` ` `aux[i] = aux[i + 1] + 1; ` ` ` `mark[arr[i]] = 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `aux[i] = aux[i + 1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 0; i < m; i++) { ` ` ` `cout << aux[q[i] - 1] << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4, 1, 2, 3, 4, 10000, 999 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `q[] = { 1, 3, 6 }; ` ` ` `int` `m = ` `sizeof` `(q) / ` `sizeof` `(q[0]); ` ` ` `printUniqueElementsInSuffix(arr, n, q, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6 6 5

## Recommended Posts:

- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- 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
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Print next greater number of Q queries
- Queries on probability of even or odd number in given ranges
- Range Queries to Find number of sub-arrays with a given xor
- Queries to answer the number of ones and zero to the left of given index
- 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
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Number of distinct subsets of a set
- Minimum product of k integers in an array of positive Integers
- Minimum number of subsets with distinct elements

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.