Queries for number of distinct elements from a given index till last index in an array
Last Updated :
16 Aug, 2021
Given a array ‘a[]’ of size n and number of queries q. Each query can be represented by a integer m. Your task is to print the number of distinct integers from index m to n i.e till last element of the array.
Examples:
Input: arr[] = {1, 2, 3, 1, 2, 3, 4, 5}, q[] = {1, 4, 6, 8}
Output: 5 5 3 1
In query 1, number of distinct integers
in a[0...7] is 5 (1, 2, 3, 4, 5)
In query 2, number of distinct integers
in a[3...7] is 5 (1, 2, 3, 4, 5)
In query 3, number of distinct integers
in a[5...7] is 3 (3, 4, 5)
In query 4, number of distinct integers
in a[7...7] is 1 (5)
Approach:
- Take an array check[] which will check if the current element is earlier visited or not. If already visited mark it as 1 otherwise 0.
- Take an array idx[] which will store the number of distinct elements from current index till last index.
- Loop from last, if current element has not been visited, mark its check as 1, store current counter in idx and increment it otherwise simply store current counter in idx.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define MAX 100001
// Function to perform queries to find
// number of distinct elements from
// a given index till last index in an array
void find_distinct(int a[], int n, int q, int queries[])
{
int check[MAX] = { 0 };
int idx[MAX];
int cnt = 1;
for (int i = n - 1; i >= 0; i--) {
// Check if current element
// already visited or not
if (check[a[i]] == 0) {
// If not visited store current counter
// and increment it and mark check as 1
idx[i] = cnt;
check[a[i]] = 1;
cnt++;
}
else {
// Otherwise if visited simply
// store current counter
idx[i] = cnt - 1;
}
}
// Perform queries
for (int i = 0; i < q; i++) {
int m = queries[i];
cout << idx[m] << " ";
}
}
// Driver code
int main()
{
int a[] = { 1, 2, 3, 1, 2, 3, 4, 5 };
int n = sizeof(a) / sizeof(int);
int queries[] = { 0, 3, 5, 7 };
int q = sizeof(queries) / sizeof(int);
find_distinct(a, n, q, queries);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int MAX =100001;
// Function to perform queries to find
// number of distinct elements from
// a given index till last index in an array
static void find_distinct(int a[], int n, int q, int queries[])
{
int []check = new int[MAX];
int []idx = new int[MAX];
int cnt = 1;
for (int i = n - 1; i >= 0; i--)
{
// Check if current element
// already visited or not
if (check[a[i]] == 0)
{
// If not visited store current counter
// and increment it and mark check as 1
idx[i] = cnt;
check[a[i]] = 1;
cnt++;
}
else
{
// Otherwise if visited simply
// store current counter
idx[i] = cnt - 1;
}
}
// Perform queries
for (int i = 0; i < q; i++)
{
int m = queries[i];
System.out.print(idx[m] + " ");
}
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 2, 3, 1, 2, 3, 4, 5 };
int n = a.length;
int queries[] = { 0, 3, 5, 7 };
int q = queries.length;
find_distinct(a, n, q, queries);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python implementation of the approach
MAX = 100001;
# Function to perform queries to find
# number of distinct elements from
# a given index till last index in an array
def find_distinct(a, n, q, queries):
check = [0] * MAX;
idx = [0] * MAX;
cnt = 1;
for i in range(n - 1, -1, -1):
# Check if current element
# already visited or not
if (check[a[i]] == 0):
# If not visited store current counter
# and increment it and mark check as 1
idx[i] = cnt;
check[a[i]] = 1;
cnt += 1;
else:
# Otherwise if visited simply
# store current counter
idx[i] = cnt - 1;
# Perform queries
for i in range(0, q):
m = queries[i];
print(idx[m], end = " ");
# Driver code
a = [ 1, 2, 3, 1, 2, 3, 4, 5 ];
n = len(a);
queries = [ 0, 3, 5, 7 ];
q = len(queries);
find_distinct(a, n, q, queries);
# This code is contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX =100001;
// Function to perform queries to find
// number of distinct elements from
// a given index till last index in an array
static void find_distinct(int []a, int n, int q, int []queries)
{
int []check = new int[MAX];
int []idx = new int[MAX];
int cnt = 1;
for (int i = n - 1; i >= 0; i--)
{
// Check if current element
// already visited or not
if (check[a[i]] == 0)
{
// If not visited store current counter
// and increment it and mark check as 1
idx[i] = cnt;
check[a[i]] = 1;
cnt++;
}
else
{
// Otherwise if visited simply
// store current counter
idx[i] = cnt - 1;
}
}
// Perform queries
for (int i = 0; i < q; i++)
{
int m = queries[i];
Console.Write(idx[m] + " ");
}
}
// Driver code
public static void Main(String[] args)
{
int []a = { 1, 2, 3, 1, 2, 3, 4, 5 };
int n = a.Length;
int []queries = { 0, 3, 5, 7 };
int q = queries.Length;
find_distinct(a, n, q, queries);
}
}
/* This code is contributed by PrinciRaj1992 */
JavaScript
<script>
// JavaScript implementation of the approach
// Function to perform queries to find
// number of distinct elements from
// a given index till last index in an array
function find_distinct(a, n, q, queries)
{
let MAX =100001;
let check = new Array(MAX).fill(0);
let idx = new Array(MAX).fill(0);
let cnt = 1;
let i=n-1;
while (i>=0) {
// Check if current element
// already visited or not
if (check[a[i]] == 0)
{
// If not visited store current counter
// and increment it and mark check as 1
idx[i] = cnt;
check[a[i]] = 1;
cnt++;
}
else
{
// Otherwise if visited simply
// store current counter
idx[i] = cnt - 1;
}
i--;
}
// Perform queries
for (let i = 0; i < q; i++)
{
let m = queries[i];
document.write(idx[m] + " ");
}
}
// Driver code
let a = [1, 2, 3, 1, 2, 3, 4, 5 ];
let n = a.length;
let queries = [0, 3, 5, 7];
let q = queries.length;
find_distinct(a, n, q, queries);
</script>
Time Complexity: O(MAX + N)
Auxiliary Space: O(MAX)
Similar Reads
Count subarrays having a single distinct element that can be obtained from a given array Given an array arr[] of size N, the task is to count the number of subarrays consisting of a single distinct element that can be obtained from a given array. Examples: Input: N = 4, arr[] = { 2, 2, 2, 2 }Output: 7Explanation: All such subarrays {{2}, {2}, {2}, {2}, {2, 2}, {2, 2, 2}, {2, 2, 2, 2}}.
5 min read
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
10 min read
Queries for number of distinct elements in a subarray | Set 2 Given an array arr[] of N integers and Q queries. Each query can be represented by two integers L and R. The task is to find the count of distinct integers in the subarray arr[L] to arr[R].Examples: Input: arr[] = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9 }, L = 0, R = 4 Output: 3Input: arr[] = { 1, 1, 2, 1, 3
12 min read
Minimum number of distinct elements present in a K-length subsequence in an array Given an array A[] consisting of N integers and an integer K, the task is to count the minimum number of distinct elements present in a subsequence of length K of the given array, A. Examples: Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 4Output: 2Explanation: The subsequence of length 4 containing mini
7 min read
Count distinct median possible for an Array using given ranges of elements Given an array of pairs arr[] which denotes the ranges of the elements of an array, the task is to count the distinct median of every possible array using these ranges. Examples: Input: arr[] = {{1, 2}, {2, 3}} Output: 3 Explanation: => If x1 = 1 and x2 = 2, Median is 1.5 => If x1 = 1 and x2 =
8 min read
Find Number of Unique Elements in an Array After each Query Given 2d array A[][1] of size N and array Q[][2] of size M representing M queries of type {a, b}. The task for this problem is in each query move all elements from A[a] to A[b] and print the number of unique elements in A[b]. Constraints: 1 <= N, Q <= 1051 <= A[i] <= 1091 <= a, b <
10 min read