A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

 


Chocolate Monger Problem Code: CM164364
Submit

Read problems statements in Mandarin ChineseRussian, and Bengali as well.

There are n chocolates, and you are given an array of n numbers where the i-th number Ai is the flavour type of the i-th chocolate. Sebrina wants to eat as many different types of chocolates as possible, but she also has to save at least x number of chocolates for her little brother.

Find the maximum possible number of distinct flavour types Sebrina can have.

Input:

The first line contains an integer T --- the number of test cases.

The first line of each test case consists of two integers nx - The number of chocolates Sabrina has and the number of chocolates she has to save for her brother, respectively.

The second line contains n integers A1,,An, where the i-th chocolate has type Ai.

Output:

For each test case, output a single integer denoting the maximum possible number of distinct chocolate flavours Sabrina can eat.

Constraints

1T10

1xn2105

1Ai109

Sum of n over all test cases do not exceed 2105

Sample Input 1 

3
2 1
1 2
4 2
1 1 1 1
5 3
50 50 50 100 100

Sample Output 1 

1
1
2

Explanation

In the first test case, she can give any 1 chocolate to her brother and can have the other for herself resulting in 1 flavour type for Sebrina.

solution:

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long

void solve()
{
    ll n,x;
    cin>>n>>x;
    vector<ll> v(n);
    for(ll i=0;i<n;i++)
    {
        cin>>v[i];
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    ll s=v.size();
    if(s>n-x)
    {
        s=n-x;
    }
    cout<<s<<"\n";
}

int main() {
	int t;
	cin>>t;
	while(t--)
	{
	    solve();
	}
	return 0;
}

No comments:

Post a Comment