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: import java.io.*;
import java.util.*;

class Main {
	static void calculate(int arr[],int n,int freq[]){
		Stack<Integer>st = new Stack<>();
		st.push(0);
		int res[] = new int[n];
		for(int i=n-1;i>=0;i--){
			while(!st.isEmpty() && freq[st.peek()]<=freq[arr[i]]){
				st.pop();
			}
			if(st.isEmpty()){
				res[i] = -1;
			} else {
				res[i] = st.peek();
			}
			st.push(arr[i]);
		}
		for(int i=0;i<n;i++){
			System.out.print(res[i]+" ");
		}
	}
	public static void main (String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int arr[] = new int[n];
		int max = -1;
		String srr[] = br.readLine().split("\\s");
		for(int i=0;i<n;i++){
			arr[i] = Integer.parseInt(srr[i]);
			if(arr[i]>max){
				max = arr[i];
			}
		}
		int freq[] = new int[max+1];
		for(int i=0;i<max+1;i++){
			freq[i] = 0;
		}
		for(int i=0;i<n;i++){
			freq[arr[i]]++;
		}
		calculate(arr,n,freq);
	}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?