Chocolate Monger Problem Code: CM164364Submit
Read problems statements in Mandarin Chinese, Russian, and Bengali as well.
There are chocolates, and you are given an array of numbers where the -th number is the flavour type of the -th chocolate. Sebrina wants to eat as many different types of chocolates as possible, but she also has to save at least 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 --- the number of test cases.
The first line of each test case consists of two integers , - The number of chocolates Sabrina has and the number of chocolates she has to save for her brother, respectively.
The second line contains integers , where the -th chocolate has type .
Output:
For each test case, output a single integer denoting the maximum possible number of distinct chocolate flavours Sabrina can eat.
Constraints
Sum of over all test cases do not exceed
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