ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: java

Code 1:





import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestFile {
	public static void main(String[]args) {
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		while( t-- > 0) {
			int n = scanner.nextInt();
			int k = scanner.nextInt();
			int[] arr = new int[n];
			HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
			for(int i=0;i<n;i++) {
				arr[i] = scanner.nextInt();
				if(map.containsKey(arr[i])) {
					map.put(arr[i], map.get(arr[i]) + 1);
				} else {
					map.put(arr[i], 1);
				}
			}
			if(k == 0) {
				System.out.println(map.size());
				continue;
			}
			List<Integer> list = new ArrayList<Integer>();
			for(Map.Entry<Integer, Integer> elEntry : map.entrySet()) {
				list.add(elEntry.getValue());
			}
			list.sort(null);
			//System.out.println(list);
			int count = 1;
			for(int i=0;i<list.size();i++) {
				if(k >= list.get(i)) {
					k -= list.get(i);
					list.set(i, 0);
				} else {
					count = list.size() - i;
					break;
				}
			}
			System.out.println(count);
		}
		scanner.close();
	}
	
}


Code 2:





import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestFile {
	public static void main(String[]args) {
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		while( t-- > 0) {
			int n = scanner.nextInt();
			int k = scanner.nextInt();
			int[] arr = new int[n];
			HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
			for(int i=0;i<n;i++) {
				arr[i] = scanner.nextInt();
				if(map.containsKey(arr[i])) {
					map.put(arr[i], map.get(arr[i]) + 1);
				} else {
					map.put(arr[i], 1);
				}
			}
			if(k == 0) {
				System.out.println(map.size());
				continue;
			}
			List<Integer> list = new ArrayList<Integer>();
			for(Map.Entry<Integer, Integer> elEntry : map.entrySet()) {
				list.add(elEntry.getValue());
			}
			list.sort(null);
			System.out.println(list);
			int count = 1;
			for(int i=0;i<list.size();i++) {
				if(k >= list.get(i)) {
					k -= list.get(i);
					list.set(i, 0);
				} else {
					count = list.size() - i;
					break;
				}
			}
			System.out.println(count);
		}
		scanner.close();
	}
	
}


Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++23 

Code 1:
/* 
 * Gorrila and the Exam
 * 18.01.24
 */
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

// calc total subarrays with same element -> calc subarrays whose sum of lenth is equal to p -> subtract
int solve(int n, int k, vector<int>& a){
    

    unordered_map<int, int> mp;
    for(int i = 0; i < a.size(); i++){
        mp[a[i]]++;
    }
    vector<int> fq;
    for(auto& a: mp){
        fq.push_back(a.second);
    }

    sort(fq.begin(), fq.end());
    int m = fq.size();
    int ans = 0;
    for(int i = 0; i < m-1; i++){
        if(fq[i] > k){
            return m - i;
            k = 0;
        }
        else {
            k -= fq[i];
        }
    }
    return 1;

}

int main(){
    int tc; 
    cin >> tc;
    while(tc--){
        int n, k; 
        cin >> n >> k; 
        vector<int> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        int ans = solve(n, k, a); 
        cout << ans << endl;
    }

    return 0; 
}


Code 2:
/* 
 * Gorrila and the Exam
 * 18.01.24
 */
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

// calc total subarrays with same element -> calc subarrays whose sum of lenth is equal to p -> subtract
int solve(int n, int k, vector<int>& a){
    
/*
    unordered_map<int, int> mp;
    for(int i = 0; i < a.size(); i++){
        mp[a[i]]++;
    }

    Using a vector instead of unordered_map is more efficient 
    due to reduced overhead and better memory locality, avoiding hash collision
*/
    sort(a.begin(), a.end());
    vector<int> cnt = {1};

     for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1]) {
            cnt.back()++;
        } else {
            cnt.emplace_back(1);
        }
    }

    sort(cnt.begin(), cnt.end());
    int m = cnt.size();
    int ans = 0;
    for(int i = 0; i < m-1; i++){
        if(cnt[i] > k){
            return m - i;
            k = 0;
        }
        else {
            k -= cnt[i];
        }
    }
    return 1;

}

int main(){
    int tc; 
    cin >> tc;
    while(tc--){
        int n, k; 
        cin >> n >> k; 
        vector<int> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        int ans = solve(n, k, a); 
        cout << ans << endl;
    }

    return 0; 
}


Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Code 1:
#include<bits/stdc++.h>
using namespace std;

int solve(){
    int n,k;
    vector<int> a;
    cin>>n>>k;
    for(int i = 0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    priority_queue<int,vector<int>,greater<int>> pq;
    map<int,int> mp;
    for(auto x : a){
        mp[x]++;
    }
    int ele = mp.size();
    if(ele >= k){
        return 1;
    }
    for(auto x : mp){
        pq.push(x.second);
    }
    while(k>=pq.top()){
        int p = pq.top();
        pq.pop();
        k -= p;
    }
    return pq.size();
}

    int main(){
    int t;
    cin>>t;
    while(t--){
        int ans = solve();
        cout<<ans<<endl;
    }
    return 0;
}

Code 2:
#include<bits/stdc++.h>
using namespace std;

int solve(){
    int n,k;
    vector<int> a;
    cin>>n>>k;
    for(int i = 0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    priority_queue<int,vector<int>,greater<int>> pq;
    map<int,int> mp;
    for(auto x : a){
        mp[x]++;
    }
    for(auto x : mp){
        pq.push(x.second);
    }
    while(!pq.empty() && k>=pq.top()){
        int p = pq.top();
        pq.pop();
        k -= p;
    }
    return pq.empty() ? 1 : pq.size();
}

    int main(){
    int t;
    cin>>t;
    while(t--){
        int ans = solve();
        cout<<ans<<endl;
    }
    return 0;
}

Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++23 

Code 1:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; 
void solve()
{
	int n,k;cin>>n>>k;
	vector<int>a(n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	vector<int>b(1,1);
	for(int i=1;i<n;i++)
	{
		if(a[i]==a[i-1])
		{
			b.back()++;
		}
		else
		b.push_back(1);
	}
	sort(b.begin(),b.end());
	int m=b.size();
	for(int i=0;i<m;i++)
	{
		if(b[i]>k)
		{
			cout<<m-i<<endl;return;
		}
		k-=b[i];
	}
	cout<<"1"<<endl;
}
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	int t;cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


Code 2:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; 
void solve()
{
	int n,k;cin>>n>>k;
	vector<int>a(n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	vector<int>b(1,1);
	for(int i=1;i<n;i++)
	{
		if(a[i]==a[i-1])
		{
			b.back()++;
		}
		else
		b.push_back(1);
	}
	sort(b.begin(),b.end());
	int m=b.size();
	for(int i=0;i<m;i++)
	{
		if(b[i]>k)
		{
			cout<<m-i<<endl;return;
		}
		k-=b[i];
	}
	cout<<"1"<<endl;
}
int main()
{
	ios::sync_with_stdio(false);cin.tie(false);
	int t;cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++20 

Code 1:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<unordered_map>
using namespace std;
void solve() {
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector <int>q(n);
        unordered_map<int, int>q1;
        for (int i = 0; i < n; i++) {
            cin >> q[i];
            q1[q[i]]++;
        }
        vector<int>q2;
        for (const auto x : q1) {
            q2.push_back(x.second);
        }
        int a = q2.size();
        sort(q2.begin(), q2.end());
        for (int i = 0; i < q2.size(), i++) {
            if (q2[i] <= k) {
                k -= q2[i];
                a--;
            }
        }
        cout << max(1, a) << endl;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
const int mod = 1e9+10;
int n,m,q,k;
void solve(){
    cin>>n>>k;
    vector<int>v(n);
    map<int,int>mp;
    for (int i =0;i<n;i++) {
        cin>>v[i];
        mp[v[i]]++;
    }
    int cnt =0;
    vector<pair<int,int>>pa;
    for (auto i:mp) {
        pa.push_back({i.second,i.first});
    }
    sort(all(pa));
    for (int i =0;i<pa.size()-1;i++) {
        if (pa[i].first<=k) {
            cnt++;
            k-=pa[i].first;
        }
    }
    cout<<mp.size()-cnt<<endl;
}
signed main(){
    int t  =1;
    cin>>t;
    while(t--){
        solve();
    }
}

Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++23 

Code 1:
#include <bits/stdc++.h>
using namespace std;
// #define int long long

int32_t main()
{
    int t;
    cin>>t;
    while(t--) {
        int n,k; cin>>n>>k;
        map<int,int> fq;
        for(int i=0;i<n;i++) {
            int x;cin>>x;
            fq[x]++;
        }
        vector<int> v;
        for(auto& x : fq) {
            v.push_back(x.second);
        }
        sort(v.begin(),v.end(), greater<int> ());
        while(k > 0 && !v.empty()) {
            if(v.back() > k)
                break;
            k -= v.back();
            v.pop_back();
        }
        cout <<  max((int)v.size(), 1) << endl;
    }
    return 0;
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main()
{
    int t;
    cin>>t;
    while(t--) {
        int n,k; cin>>n>>k;
        unordered_map<int,int> fq;
        for(int i=0;i<n;i++) {
            int x;cin>>x;
            fq[x]++;
        }
        vector<int> v;
        for(auto& x : fq) {
            v.push_back(x.second);
        }
        sort(v.begin(),v.end(), greater<int> ());
        while(k > 0) {
            if(v.back() <= k) {
                k -= v.back();
                v.pop_back();
            }
        }
        cout <<  max((int)v.size(), 1LL) << endl;
    }
    return 0;
}

Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Code 1:
#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
// using u128 = unsigned __int128;

void solve() {
    std::unordered_map<int, int> cnt;
    std::vector<int> data;
    int n, k;
    std::cin >> n >> k;

    for(int i = 0; i < n; ++i){
        int x;
        std::cin >> x;
        cnt[x]++;
    }


    for(auto [x, c] : cnt){
        data.push_back(c);
    }

    std::sort(data.begin(), data.end());

    int i = 0;

    int ans = data.size();

    for(int i = 0; i < data.size()-1; ++i){
        if(k >= data[i]){
            k -= data[i];
            ans--;
        }
    }

    std::cout << ans << std::endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

Code 2:
#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
//using u128 = unsigned __int128;

void solve() {
    int n, k;
    std::cin >> n >> k;
    
    std::map<int, int> cnt;
    for (int i = 0; i < n; i++) {
        int a;
        std::cin >> a;
        cnt[a]++;
    }
    
    std::vector<int> c;
    for (auto [_, x] : cnt) {
        c.push_back(x);
    }
    std::sort(c.begin(), c.end());
    int ans = c.size();
    for (int i = 0; i < c.size() - 1; i++) {
        if (k >= c[i]) {
            ans--;
            k -= c[i];
        }
    }
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++20 

Code 1:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int t;cin>>t;
	while(t--){
		int n,k;cin>>n>>k;
		int st[n],sp[n]={0};
		for(int i=0;i<n;i++){
			cin>>st[i];
		}
		sort(st,st+n);
		int p=0;
		sp[0]=1;
		for(int i=1;i<n;i++){
			if(st[i]==st[i-1]){
				sp[p]+=1;
			}
			else{
				p++;
				sp[p]=1;
			}
		}
		sort(sp,sp+p);
		int ss=p;
		while(k>0&&p>=0){
			k=k-sp[ss-p];
			p--;
		}
		p++;
		if(p<=1) p=1;
		cout<<p<<endl;
	}
	
	
	return 0;
}


Code 2:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int t;cin>>t;
	while(t--){
		int n,k;cin>>n>>k;
		int st[n],sp[n]={0};
		for(int i=0;i<n;i++){
			cin>>st[i];
		}
		sort(st,st+n);
		int p=0;
		sp[0]=1;
		for(int i=1;i<n;i++){
			if(st[i]==st[i-1]){
				sp[p]+=1;
			}
			else{
				p++;
				sp[p]=1;
			}
		}
		p++;
		sort(sp,sp+p);
		for(int i=0;i<p;i++){
			//cout<<sp[i]<<"|";
		}
		int ss=p;
		while(k>0){
			if(p==0){
				break;
			}
			if(k>=sp[ss-p]){
				k=k-sp[ss-p];
			}
			else{
				break;
			}
			p--;
		}
		if(p<=1) p=1;
		cout<<p<<endl;
	}
	
	
	return 0;
}


Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++23 

Code 1:
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main() {
    map<int,int> times;
    int t,n,k,num,st=0;
    cin >>t;
    for (int i=0;i<t;i++){
        cin >>n>>k;
        for (int j=0;j<n;j++){
        cin>>num;
        if (times[num]==0) {
            times[num]=1;
            st++;     }
        else times[num]++;
        }
        multiset<int> s;
        for (auto &pair : times) {
                s.insert(pair.second);
            }
        for (int x:s){
            if (x>k) break;
            else {
                k=k-x;
                st--;
            }
        }
        if (st!=0)cout <<st<<endl;
        else cout << "1"<<endl;
        s.clear();
        times.clear();
        n=0;k=0;num=0;st=0;
    }
}

Code 2:
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main() {
    map<int,int> times;
    int t,n,k,num,st=0;
    cin >>t;
    for (int i=0;i<t;i++){
        cin >>n>>k;
        for (int j=0;j<n;j++){
        cin>>num;
        if (times[num]==0) {
            times[num]=1;
            st++;     }
        else times[num]++;
        }
        multiset<int> s;
        for (auto &pair : times) {
                s.insert(pair.second);
            }
        for (int x:s){
            if ((x>k)||((x==1)&&(k==1))) break;
            else {
                k=k-x;
                st--;
            }
        }

        cout <<st<<endl;
        s.clear();
        times.clear();
        n=0;k=0;num=0;st=0;
    }
}

Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Code 1:
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tc; cin >> tc;
    for(int t = 0; t < tc; t++){
        int n, m; cin >> n >> m;
        vector<int> nums;
        for(int i = 0; i < n; i++){ int x; cin >> x; nums.push_back(x); }
        vector<int> freq;
        sort(nums.begin(), nums.end());
        int last = -1, f = 0;
        for(int n : nums){ 
            if(last == -1){ last = n; f++; }
            else if(last == n){ f++; }
            else{ last = n; freq.push_back(f); f = 1;}
        }
        freq.push_back(f);
        sort(freq.begin(), freq.end());
        int i = 0;
        while(i < freq.size()-1){
            if(m < freq[i]){ break; }
            m -= freq[i]; i++;
        }
        cout << freq.size() - i << '\n';
    }
}

Code 2:
#include <bits/stdc++.h>
using namespace std;

int main(){
    int tc; cin >> tc;
    for(int t = 0; t < tc; t++){
        int n, m; cin >> n >> m;
        unordered_map<int, int> nums;
        for(int i = 0; i < n; i++){ int x; cin >> x; nums[x]++; }
        vector<int> freq;
        int i = 0, removed = 0, greatest = 0;
        for(auto& [key, val] : nums){ freq.push_back(val); greatest = max(greatest, val); }
        bool runThrough = false;
        while(i < freq.size()){
            if(m < freq[i]){ i++; continue; }
            if(freq[i] == greatest){ if(!runThrough){ runThrough = true; i++; continue; } }
            m -= freq[i]; removed++; i++;
        }
        cout << freq.size() - removed << '\n';
    }
}

Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Code 1:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int solve()
{
    int n, k, num;
    cin >> n >> k;

    map<int, int> mp;

    for (int i = 0; i < n; i++)
    {
        cin >> num;
        mp[num]++;
    }

    vector<int> arr;
    for (auto it : mp)
    {
        int first = it.first;
        int second = it.second;

        arr.push_back(second);
    }

    sort(arr.begin(), arr.end());

    int ans = (int)mp.size();
    for (int i = 0; i < arr.size(); i++)
    {
        if (k - arr[i] >= 0)
        {
            k = k - arr[i];
            ans--;
        }
        else
            break;
    }
    return max(ans, 1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        cout << solve() << endl;

    return 0;
}

Code 2:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int solve()
{
    int n, k, num;
    cin >> n >> k;

    unordered_map<int, int> mp;

    for (int i = 0; i < n; i++)
    {
        cin >> num;
        mp[num]++;
    }

    vector<int> arr;
    for (auto it : mp)
    {
        int first = it.first;
        int second = it.second;

        arr.push_back(second);
    }

    sort(arr.begin(), arr.end());

    int ans = (int)mp.size();
    for (int i = 0; i < n; i++)
    {
        if (k - arr[i] >= 0)
        {
            k = k - arr[i];
            ans--;
        }
        else
            break;
    }
    return max(ans, 1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        cout << solve() << endl;

    return 0;
}

Answer: 1
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Code 1:
#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction(a) cout.unsetf(ios::floatfield);cout.precision(a);cout.setf(ios::fixed,ios::floatfield);
#define ll long long
#define endl '\n'
#define gap " "
#define bug(a) cerr<< #a << " : " << a <<endl
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define tc ll t; cin>>t; while(t--)
#define pb push_back
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/(__gcd(a,b)))
const ll mod = 1000000007;
const double pi= acos(-1);
const int n=2*(1e5+10);
int main()
{
    optimize()
    tc
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n),v1;
        ll i;
        map<ll,ll>cnt;
        for(i=0;i<n;i++){
            cin>>v[i];
            cnt[v[i]]++;
        }
        ll n1=cnt.size();
        //bug(n1);
        ll c1=0;
        for(auto u:cnt){
           v1.pb(u.second);
        }
        sort(all(v1));
        for(i=0;i<n1;i++){
            if(v1[i]<=k){
                k-=v1[i];
                c1++;
            }
            else break;
        }
        if(c1==n1)cout<<0<<endl;
        else
        {
            cout<<n1-c1<<endl;
        }
    }
}

Code 2:
#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction(a) cout.unsetf(ios::floatfield);cout.precision(a);cout.setf(ios::fixed,ios::floatfield);
#define ll long long
#define endl '\n'
#define gap " "
#define bug(a) cerr<< #a << " : " << a <<endl
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define tc ll t; cin>>t; while(t--)
#define pb push_back
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((a*b)/(__gcd(a,b)))
const ll mod = 1000000007;
const double pi= acos(-1);
const int n=2*(1e5+10);
int main()
{
    optimize()
    tc
    {
        ll n,k;
        cin>>n>>k;
        vector<ll>v(n),v1;
        ll i;
        map<ll,ll>cnt;
        for(i=0;i<n;i++){
            cin>>v[i];
            cnt[v[i]]++;
        }
        ll n1=cnt.size();
        //bug(n1);
        ll c1=0;
        for(auto u:cnt){
           v1.pb(u.second);
        }
        sort(all(v1));
        for(i=0;i<n1;i++){
            if(v1[i]<=k){
                k-=v1[i];
                c1++;
            }
            else break;
        }
        if(c1==n1)cout<<1<<endl;
        else
        {
            cout<<n1-c1<<endl;
        }
    }
}

Answer: 2
Problem: 2057B - Gorilla and the Exam
Language: c++20 

Code 1:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vector<int> a(n);
        unordered_map<int, int> mpp;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            mpp[a[i]]++;

        }
        int count = 0;
        int maxi = 1e10;
        vector<int> freq;
        for (auto it : mpp)
        {
            count++;
            maxi = max(maxi, it.second);
            freq.push_back(it.second);
        }
        sort(freq.begin(), freq.end());
        for (int i = 0; i < freq.size(); i++)
        {
            if (freq[i] <= k)
            {
                k -= freq[i];
                count--;
            }
            if(k<0) break;
        }
        if (count == 0)
            cout << 1 << endl;
        else
            cout << count << endl;
    }
    return 0;
}

Code 2:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        int x;
        map<int, int> mpp;
        for (int i = 0; i < n; i++)
        {
            cin >> x;
            mpp[x]++;

        }
        int count = 0;
        vector<int> freq;
        for (auto it : mpp)
        {
            count++;
            freq.push_back(it.second);
        }
        sort(freq.begin(), freq.end());
        for (int i = 0; i < freq.size(); i++)
        {
            if (freq[i] <= k)
            {
                k -= freq[i];
                count--;
            }
            if(k<0) break;
        }
        if (count == 0)
            cout << 1 << endl;
        else
            cout << count << endl;
    }
    return 0;
}

Answer: 2