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

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


        }
    }
}



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

public class GorillaExam {
    static int mint = (int) 1e6 + 7;
    static int mod_i = (int) 1e9 + 7;
    static long mod_l = (long) 1e12 + 7;
    public static void main(String[] args) throws IOException {
        //Scanner sc = new Scanner(System.in);
        Kattio io = new Kattio(System.in, System.out);
        int t = io.getInt();
        for (int i = 0; i < t; i++) {
            int n = io.getInt();
            int k = io.getInt();
            Map<Integer, Integer> hm = new TreeMap<>();
            for (int j = 0; j < n; j++) {
                int temp = io.getInt();
                hm.put(temp, hm.getOrDefault(temp, 0)+1);
            }
            List<Integer> cnt = new ArrayList<>(hm.values());
            Collections.sort(cnt);
//            System.out.println(cnt);
            for(int j=0;j<cnt.size();j++){
//                System.out.println(cnt.get(j)+" "+k);
                int affect = Math.min(cnt.get(j), k);
                cnt.set(j, cnt.get(j)-affect);
                k-=affect;
            }
//            System.out.println(cnt);
            int ans = 0;
            for (int val: cnt) {
                if(val!=0) ans++;
            }
//            System.out.println(ans);
            System.out.println(ans==0?1:ans);
        }
        io.close();
    }
}
class Pair {
    int x;
    long y;
    public Pair(int x, long y) {
       this.x = x;
       this.y = y;
    }
}
class Tuple {
    int x;
    long y;
    int z;
    public Tuple(int x, long y, int z) {
       this.x = x;
       this.y = y;
       this.z = z;
    }
}
class Kattio extends PrintWriter {
    public Kattio(InputStream i) {
        super(new BufferedOutputStream(System.out));
        r = new BufferedReader(new InputStreamReader(i));
    }
    public Kattio(InputStream i, OutputStream o) {
        super(new BufferedOutputStream(o));
        r = new BufferedReader(new InputStreamReader(i));
    }


    public boolean hasMoreTokens() {
        return peekToken() != null;
    }

    public int getInt() {
        return Integer.parseInt(nextToken());
    }

    public double getDouble() {
        return Double.parseDouble(nextToken());
    }

    public long getLong() {
        return Long.parseLong(nextToken());
    }

    public String getWord() {
        return nextToken();
    }


    private BufferedReader r;
    private String line;
    private StringTokenizer st;
    private String token;

    private String peekToken() {
        if (token == null)
            try {
                while (st == null || !st.hasMoreTokens()) {
                    line = r.readLine();
                    if (line == null) return null;
                    st = new StringTokenizer(line);
                }
                token = st.nextToken();
            } catch (IOException e) { }
        return token;
    }

    private String nextToken() {
        String ans = peekToken();
        token = null;
        return ans;
    }
}