using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
public
static
List<
int
> findindexes(List<
int
> arr,
int
M, List<
int
> query)
{
Dictionary<
int
, List<
int
>> fre =
new
Dictionary<
int
, List<
int
>>();
int
n = arr.Count;
List<
int
> ans =
new
List<
int
>();
int
i;
for
(i = 0 ; i < n ; i++) {
if
(!fre.ContainsKey(arr[i])){
fre.Add(arr[i],
new
List<
int
>());
}
fre[arr[i]].Add(i + 1);
}
SortedSet<pair> st =
new
SortedSet<pair>(
new
Comp());
foreach
(KeyValuePair<
int
, List<
int
>> it
in
fre){
st.Add(
new
pair(it.Value.Count, it.Key));
}
int
maxelement = 0;
i = 0;
foreach
(pair it
in
st) {
if
(i >= M)
break
;
maxelement = it.second;
i++;
}
for
(i = 0 ; i < query.Count ; i++) {
if
(!fre.ContainsKey(maxelement))
{
ans.Add(-1);
}
else
{
if
(fre[maxelement].Count < query[i])
ans.Add(-1);
else
{
ans.Add(fre[maxelement][query[i] - 1]);
}
}
}
return
ans;
}
public
static
void
Main(
string
[] args){
List<
int
> arr =
new
List<
int
>{
1, 2, 20, 8, 8, 1, 2, 5, 8, 0, 6, 8, 2
};
int
M = 1;
List<
int
> query =
new
List<
int
>{
100, 4, 2
};
List<
int
> ans = findindexes(arr, M, query);
for
(
int
i = 0 ; i < ans.Count ; i++) {
Console.Write(ans[i] +
" "
);
}
}
}
public
class
pair{
public
int
first{
get
;
set
; }
public
int
second{
get
;
set
; }
public
pair(
int
first,
int
second){
this
.first = first;
this
.second = second;
}
}
class
Comp : IComparer<pair>{
public
int
Compare(pair o2,pair o1){
if
(o1.first == o2.first){
return
o1.second - o2.second;
}
return
o1.first - o2.first;
}
}