# Queries for number of distinct elements in a subarray

Given a array ‘a[]’ of size n and number of queries q. Each query can be represented by two integers l and r. Your task is to print the number of distinct integers in the subarray l to r.
Given a[i] <= 106
Examples:

```Input : a[] = {1, 1, 2, 1, 3}
q = 3
0 4
1 3
2 4
Output :3
2
3
In query 1, number of distinct integers
in a[0...4] is 3 (1, 2, 3)
In query 2, number of distinct integers
in a[1..3] is 2 (1, 2)
In query 3, number of distinct integers
in a[2..4] is 3 (1, 2, 3)
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to use Binary Indexed Tree

1. Step 1 : Take an array last_visit of size 10^6 where last_visit[i] holds the rightmost index of the number i in the array a. Initialize this array as -1.
2. Step 2 : Sort all the queries in ascending order of their right end r.
3. Step 3 : Create a Binary Indexed Tree in an array bit[]. Start traversing the array ‘a’ and queries simultaneously and check if last_visit[a[i]] is -1 or not. If it is not, update the bit array with value -1 at the idx last_visit[a[i]].
4. Step 4 : Set last_visit[a[i]] = i and update the bit array bit array with value 1 at idx i.
5. Step 5 : Answer all the queries whose value of r is equal to i by querying the bit array. This can be easily done as queries are sorted.

 `// C++ code to find number of distinct numbers ` `// in a subarray ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX = 1000001; ` ` `  `// structure to store queries ` `struct` `Query ` `{ ` `    ``int` `l, r, idx; ` `}; ` ` `  `// cmp function to sort queries according to r ` `bool` `cmp(Query x, Query y) ` `{ ` `    ``return` `x.r < y.r; ` `} ` ` `  `// updating the bit array ` `void` `update(``int` `idx, ``int` `val, ``int` `bit[], ``int` `n) ` `{ ` `    ``for` `(; idx <= n; idx += idx&-idx) ` `        ``bit[idx] += val; ` `} ` ` `  `// querying the bit array ` `int` `query(``int` `idx, ``int` `bit[], ``int` `n) ` `{ ` `    ``int` `sum = 0; ` `    ``for` `(; idx>0; idx-=idx&-idx) ` `        ``sum += bit[idx]; ` `    ``return` `sum; ` `} ` ` `  `void` `answeringQueries(``int` `arr[], ``int` `n, Query queries[], ``int` `q) ` `{ ` `    ``// initialising bit array ` `    ``int` `bit[n+1]; ` `    ``memset``(bit, 0, ``sizeof``(bit)); ` ` `  `    ``// holds the rightmost index of any number ` `    ``// as numbers of a[i] are less than or equal to 10^6 ` `    ``int` `last_visit[MAX]; ` `    ``memset``(last_visit, -1, ``sizeof``(last_visit)); ` ` `  `    ``// answer for each query ` `    ``int` `ans[q]; ` `    ``int` `query_counter = 0; ` `    ``for` `(``int` `i=0; i

Output:

```3
2
3
```

This article is contributed by Ayush Jha. 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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.