给定一个大小为 n 的数组 ‘a[]’ 和查询数 q。每个查询都可以用一个整数 m 表示。您的任务是打印从索引 m 到 n 的不同整数的数量,即直到数组的最后一个元素。
例子:
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)
方法:
- 以数组check[]为例,它会检查当前元素是否较早被访问过。如果已经访问过将其标记为1否则为0 。
- 取一个数组idx[] ,它将存储从当前索引到最后一个索引的不同元素的数量。
- 从最后一个循环,如果当前元素未被访问,则将其检查标记为1 ,将当前计数器存储在idx 中并将其递增,否则只需将当前计数器存储在idx 中。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
5 5 3 1
时间复杂度: O(MAX + N)
辅助空间: O(MAX)