For this Question: You are given an array A of size N. For each of the indices i (1 &le; i &le; N), find the the smallest index j such that i < j and the frequency of A<sub>i</sub> is lesser than or equal to that of A<sub>j</sub> in the array.

<b>Note</b>
Consider 1-based indexingThe first line of the input contains a single integer N.
The second line of the input contains N space-separated integers.

<b>Constraints:</b>
1 &le; N &le;10<sup>5</sup>
1 &le; A<sub>i</sub> &le; 10<sup>5</sup>For each of the indices i (1 &le; i &le; N), print the smallest index j such that i < j and the frequency of A<sub>i</sub> is lesser than or equal to that of A<sub>j</sub> in the array. If there is no such index j, print -1 for that particular index i.Sample Input:
7
1 2 3 3 3 1 1

Sample Output:
3 3 4 5 6 7 -1

<<b>Explanation:</b>
For,
i = 1, j = 3
i = 2, j = 3
i = 3, j = 4
i = 4, j = 5
i = 5, j = 6
i = 6, j = 7
i = 7, no such j exists, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
    public static void main (String[] args) {
        Scanner sc= new Scanner(System.in);
        int n= sc.nextInt();
        int[] arr= new int[n];
        for(int i=0;i<n;i++){
            arr[i]= sc.nextInt();
        }

        HashMap<Integer, Integer> hm= new HashMap<>();
        for(int i=0; i<n; i++){
            if(!hm.containsKey(arr[i])){
                hm.put(arr[i], 1);
            }
            else{
                hm.put(arr[i], hm.get(arr[i])+1);
            }
        }

        Stack<Integer> stack= new Stack<>();
        int[] ans = new int[n];
        for(int i=0; i<n; i++){
            while(!stack.isEmpty() && hm.get(arr[i]) >= hm.get(arr[stack.peek()])){
                ans[stack.pop()] = i+1;
            }
            stack.push(i);
        }
        while(!stack.isEmpty()){
            ans[stack.pop()]= -1;
        }
        for(int i=0; i<n; i++){
            System.out.print(ans[i]+" ");
        }
    }
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?