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

Solution 1 (runtime: 858 ms):

import java.sql.SQLOutput;
import java.util.*;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static void no() {
        System.out.println("NO");
        return;
    }

    public static void yes() {
        System.out.println("YES");
        return;
    }

    public static void array_input(int[] arr, int size) {
        for (int i = 0; i < size; i++) {
            arr[i] = sc.nextInt();
        }
    }

    public static void array_output(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void matrix_input(int[][] arr, int rows, int colm) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < colm; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
    }

    public static void matrix_output(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j]);
            }
        }
    }

//main code starts from line 43


    // main code starts from here
    public static int mex(int[] arr) {
        Arrays.sort(arr);
        int ans = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == ans) {
                ans++;
            }
        }
        return ans;
    }
    public static int isBalanced(int [] arr){
        int ans=0;
        if(arr.length==0 || arr.length==1){ans=1;}
        else{
            if(arr.length%2==0 && arr[arr.length/2]==1){
                ans=1;
            }
            else{
                if(arr[(arr.length/2)-1]==0 && arr[(arr.length/2)]==1){
                    ans=1;
                }
                else if(arr[(arr.length/2)+1]==1 && arr[(arr.length/2)]==0){
                    ans=1;
                }
            }
        }
        return ans;
    }


    public static void main(String[] args) {

        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int k=sc.nextInt();
            int []a=new int[n];
            array_input(a,n);
            Arrays.sort(a);
            ArrayList<Integer> fq=new ArrayList<>();
            int count = 1;

            // Iterate through the list to count frequencies
            for (int i = 0; i < n; i++) {
                if (i + 1 < n && a[i]==a[i+1]){
                    count++;
                } else {
                    fq.add(count);
                    count = 1;
                }
            }
            Collections.sort(fq);
            
            int l=0;
            while (!fq.isEmpty()){
                if(k>=fq.get(l)){

                    k=k-fq.get(l);
                    fq.remove(l);

                }
                else break;
            }


            if(fq.isEmpty()){
                System.out.println("1");
            }
            else {
                System.out.println(fq.size());
            }





            


            


        }


    }
}



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);


        }
    }
}