Problem: 2057B - Gorilla and the Exam
Language: java

Solution 1 (runtime: 390 ms):
import java.util.*;
import java.io.*;

public class GorillaandtheExam {

    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int k = sc.nextInt();

            HashMap<Integer,Integer> map=new HashMap<>();
            int[] arr = new int[n];
            arr[0]=sc.nextInt();
            int maxVal=arr[0];
            map.put(arr[0],map.getOrDefault(arr[0],0)+1);
            for (int i = 1; i < n; i++) {
                arr[i] = sc.nextInt();
                map.put(arr[i],map.getOrDefault(arr[i],0)+1);
                if(map.get(arr[i])>map.get(maxVal)){
                    maxVal=arr[i];
                }
            }

            HashMap<Integer,Integer> sortedMap=new HashMap<>();
            sortedMap=sortMap(map);

//            System.out.println(sortedMap);
//            System.out.println(maxVal);

            int count=map.size();
            for(Map.Entry<Integer,Integer> entry:sortedMap.entrySet()){
                if(count==1) break;
                int value=entry.getValue();
                if(k>=value){
                    k-=value;
                    count--;
                }
                else break;


            }

            System.out.println(count);
        }
    }

    public static HashMap<Integer,Integer> sortMap(HashMap<Integer,Integer> map){

        List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(map.entrySet());

        // Step 3: Sort the list by values
        entryList.sort(Map.Entry.comparingByValue());

        // Step 4: Create a LinkedHashMap to maintain the sorted order
        LinkedHashMap<Integer, Integer> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<Integer, Integer> entry : entryList) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

//        // Display the sorted map
//        for (Map.Entry<Integer, Integer> entry : sortedMap.entrySet()) {
//            System.out.println(entry.getKey() + " -> " + entry.getValue());
//        }

        return sortedMap;
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Solution 2 (runtime: 718 ms):
import java.util.*;

public class cfhello2025B 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ret[] = new int[n];
        for(int i = 0;i<n;i++)
        {
            int size = sc.nextInt();
            int k = sc.nextInt();
            int temp[] = new int[size];
            Map<Integer,Integer> store = new HashMap<>();
            for(int j = 0;j<size;j++)
            {
                temp[j] = sc.nextInt();
                if(store.containsKey(temp[j]))
                {
                    store.put(temp[j],store.get(temp[j])+1);
                }
                else
                {
                    store.put(temp[j],1);
                }
            }
            PriorityQueue<Integer> occ = new PriorityQueue<>();
            for (Map.Entry<Integer, Integer> entry : store.entrySet()) 
            {
                occ.add(entry.getValue());
            }
            while(k!=0)
            {
                if(occ.size() == 0)
                {
                    break;
                }
                int smallest = occ.poll();
                if(k>=smallest)
                {
                    k-=smallest;
                }
                else
                {
                    occ.add(smallest);
                    break;
                }
            }
            ret[i] = occ.size();
            if(occ.size() == 0)
            {
                ret[i] = 1;
            }
        }
        for(int i = 0;i<ret.length;i++)
        {
            System.out.println(ret[i]);
        }
    }
}