For this Question: Given an array of N elements, for each element find the value of nearest element to the right which is having frequency greater than as that of current element. If there does not exist an answer for a position, then make the value ‘-1’.Firs line of input contains the size of the array N, next line contains N space separated integers depicting values of the array.

Constraints:-
1 < = N < = 100000
1 < = Arr[i] < = 100000For each element print the value of nearest element to the right which is having frequency greater than as that of current element. If there does not exist an answer for a position, then make the value ‘-1’Sample Input:-
6
1 2 1 3 2 1

Sample Output:-
-1 1 -1 2 1 -1

Sample Input:-
6
1 2 2 3 3 3

Sample Output:-
2 3 3 -1 -1 -1 , I have written this Solution Code: 
#include <bits/stdc++.h>


using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))

#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)

#define IN(A, B, C) assert( B <= A && A <= C)

#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)

#define FOR1(i,j,a) for(int i=j;i<a;i++)

#define EB emplace_back

#define INF (int)1e9

#define EPS 1e-9

#define PI 3.1415926535897932384626433832795

#define MOD 1000000007

#define read(type) readInt<type>()
#define max2 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long

	int freq[max2];
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}

void NFG(int a[], int n)
{

	// stack data structure to store the position
	// of array element
	stack<int> s;
	s.push(0);

	int res[n] = {0};
	for (int i = 0; i < n; i++)
	{


		if (freq[a[s.top()]] > freq[a[i]])
			s.push(i);
		else
		{


			while (s.size()!=0 && freq[a[s.top()]] < freq[a[i]])
			{

				res[s.top()] = a[i];
				s.pop();
			}
			// now push the current element
			s.push(i);
		}

	}

	while (!s.empty())
	{
		res[s.top()] = -1;
		s.pop();
	}
	for (int i = 0; i < n; i++)
	{
		// Print the res list containing next
		// greater frequency element
		cout << res[i] << " ";
	}
}

//Driver code

signed  main()
{
fast();

	int n;
	cin>>n;
	int a[n];
	FOR(i,n){
	cin>>a[i];}

	int max1 = 0;
	for (int i = 0; i < n; i++)
	{
		//Getting the max element of the array
		if (a[i] > max1)
		{
			max1 = a[i];
		}
	}

	//Calculating frequency of each element
	for (int i = 0; i < n; i++)
	{
		freq[a[i]]++;
	}

	NFG(a, n);
	return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not?