using System;
using System.Collections.Generic;
class GFG {
static Stack<Tuple<int,int>> mystack = new Stack<Tuple<int,int>>();
static Dictionary<int, int> mymap = new Dictionary<int, int>();
static void NGF(int[] arr,int[] res) {
int n = arr.Length;
for(int i = 0; i < n; i++) {
if(mymap.ContainsKey(arr[i]))
mymap[arr[i]] = mymap[arr[i]] + 1;
else
mymap[arr[i]] = 1;
}
int curr_freq = mymap[arr[n-1]];
mystack.Push(new Tuple<int,int>(arr[n-1],curr_freq));
res[n-1] = -1;
for(int i = n-2;i>=0;i--) {
curr_freq = mymap[arr[i]];
while(mystack.Count > 0 && curr_freq >= mystack.Peek().Item2)
mystack.Pop();
res[i] = (mystack.Count == 0) ? -1 : mystack.Peek().Item1;
mystack.Push(new Tuple<int,int>(arr[i],mymap[arr[i]]));
}
}
static void Main() {
int[] arr = {1, 1, 1, 2, 2, 2, 2, 11, 3, 3};
int[] res = new int[arr.Length];
NGF(arr, res);
Console.Write("[");
for(int i = 0; i < arr.Length - 1; i++)
{
Console.Write(res[i] + ", ");
}
Console.Write(res[arr.Length - 1] + "]");
}
}