Problem: Given a list (combination of repeated and distinct elements) and number of elements deletion X. You have to delete any X elements from the list so that list will have minimum distinct number and return the length of the minimum distinct list.
Input Format: The first line contains an integer T. T is the number of independent testcases. For each testcase the first line contains an integer denoting the number of elements you can delete. Next line contains the list. (Inputs are separated by space here.)
Sample Input:
2
2
1 1 2 4
3
1 1 1 2 2 2 4 5 6
Sample Output:
1
2
Explanation: Here, 2 and 4 are removed so that it has the minimum distinct element 1 which is repeated (repetition are removed) so the length of the minimum distinct list is 1
For the second case, 4, 5 and 6 are removed so that it has the minimum distinct element 1 and 2 which is repeated (repetition are removed) so the length of the minimum distinct list is 2.
Solution: We strongly recommend you to try the problem first before moving to the solution.
Python
# Code to understand the Minimum distinct element problem in Python
# www.codewindow.in
def solve():
rem=int(input())
list_num=list(map(int, input().split(" ")))
list_element_count=[]
set_distinct=set(list_num)
for i in set_distinct:
count=list_num.count(i)
list_element_count.append(count)
list_element_count.sort()
length=len(list_element_count)
for count in list_element_count:
if count<=rem:
rem -= count
length -= 1
else:
break
print(length)
# Driver code
t=int(input())
while(t):
solve()
t-=1
C++ (STL)
/* CPP solution */
/* www.codewindow.in */
#include <bits/stdc++.h>
using namespace std;
void solve() {
int x, c=0;
string s;
vector<int> v;
cin >> x;
cin.ignore(numeric_limits<streamsize>::max(),'\n'); // ignore the "\n"
getline(cin, s);
for(int i=0; i<s.length(); i+=2) {
int temp=(int)s[i]-48;
v.push_back(temp); // push the elemnts as int in a vector
}
sort(v.begin(), v.end()); // Sort the vector
for(int i=0; i<v.size() && x>0; i+=c) {
c=count(v.begin(), v.end(), v[i]); // count frequency
if(c==1) {
v.erase(v.begin()+i); // If unique then remove it until x=0
--x;
c=0;
}
}
auto it=unique(v.begin(), v.end()); // Iterator for unique elements
v.resize(distance(v.begin(), it)); // Resize the vector by removing duplicates
cout << v.size() << "\n"; // Print the sizeof vector
}
// Driver code
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1; // Testcases
cin >> t;
while(t--)
solve();
return 0;
}
Input:
2
2
1 1 2 4
3
1 1 1 2 2 2 4 5 6
Output:
1
2
Follow Us
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in