Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Author: Daanish Mahajan
Tester: Manan Grover
Editorialist: Aman Dwivedi
DIFFICULTY:
Simple
PREREQUISITES:
None
PROBLEM:
You are an administrator of a popular quiz website. Every day, you hold a quiz on the website. Each user is allowed to attempt a quiz at most once. Each user has a unique id from [1, N]. Also, there are M admins for the contest having a unique id from [N+1, N+M]. Sometimes admins too attempt the quizzes for testing purposes. Thus, they may attempt a quiz multiple times
Given a list L of ids for the K total submissions, count the number of users who attempted the quiz more than once to disqualify them and return a list of their ids in ascending order.
EXPLANATION:
The problem is straightforward, it wants us to find the count of users who attempted the quiz more than once and return a list of their ids in ascending order.
Since we are given the list L of ids for the K total submissions, we can easily find the number of times the user had attempted the quiz. This can be done by creating the frequency array of size N where i^{th} element of array denotes the user which has id i.
Now, we can traverse the list L, and If the id of the submission is less than N (since id above than N are admins), we will increment the number of submissions for this id in our frequency array by 1.
Pseudo code for same
// frequency array each intialized to 0
int freq[n+1]={};
// Traverse the list L
for(int i=0;i<k;i++)
{
if(l[i]<=n)
freq[l[i]]++;
}
Finally, traverse the frequency array, and if we find any id having more than 1 submission, then we need to disqualify this user and hence put this user id in our answer. At last, we output the number of users who were disqualified and their ids.
TIME COMPLEXITY:
O(K) per test case
SOLUTIONS:
Setter
#include<bits/stdc++.h>
# define pb push_back
#define pii pair<int, int>
#define mp make_pair
# define ll long long int
using namespace std;
int main()
{
int t; cin >> t;
int n, m, k;
while(t--){
cin >> n >> m >> k;
int f[n + m + 1]; memset(f, 0, sizeof(f));
int x;
for(int i = 1; i <= k; i++){
cin >> x;
f[x]++;
}
vector<int> ans;
for(int i = 1; i <= n; i++){
if(f[i] > 1)ans.pb(i);
}
int sz = ans.size();
cout << sz;
for(int i = 0; i < sz; i++){
cout << " " << ans[i];
}
cout << endl;
}
}
Tester
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n,m,k;
cin>>n>>m>>k;
vector<int> ans;
int a[n+1] = {};
for(int i = 0; i < k; i++){
int temp;
cin>>temp;
if(temp > n){
continue;
}
if(a[temp] == 1){
ans.push_back(temp);
}
a[temp]++;
}
sort(ans.begin(), ans.end());
cout<<ans.size()<<" ";
for(int i = 0; i < ans.size(); i++){
cout<<ans[i]<<" ";
}
cout<<"\n";
}
return 0;
}
Editorialist
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n,m,k;
cin>>n>>m>>k;
map<int,int> m1;
vector <int> ans;
for(int i=0;i<k;i++)
{
int x;
cin>>x;
if(x<=n && m1[x]==1)
ans.push_back(x);
m1[x]++;
}
cout<<ans.size()<<" ";
sort(ans.begin(),ans.end());
for(auto itr: ans)
cout<<itr<<" ";
cout<<"\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--)
solve();
return 0;
}