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

Solution 1 (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]);
        }
    }
}


Solution 2 (runtime: 421 ms):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class B {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        String next() {
            while (st == null || !st.hasMoreElements()) {
                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 {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int t = sc.nextInt();
        while (t != 0) {
            t--;
            int n = sc.nextInt();
            int k = sc.nextInt();
            long[] arr=new long[n];
            HashMap<Long,Long> map=new HashMap<>();
            for (int i = 0; i <n ; i++) {
                arr[i]= sc.nextLong();
                map.put(arr[i],map.getOrDefault(arr[i],(long)0 )+1);
            }
            ArrayList<Long> freq=new ArrayList<>();
            for(long key:map.keySet()){
                freq.add(map.get(key));
            }
            Collections.sort(freq);
            long ans=1;
            long rem=n-(freq.get(freq.size()-1)+k);
            if (rem>0) {
                for (int i = freq.size() - 2; i >= 0; i--) {
                     rem-=freq.get(i);
                     ++ans;
                     if (rem<=0){
                         break;
                     }
                }
            }
            System.out.println(ans);


        }
    }
}