Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For \operatorname{MEX}(a)=k, don't think about the whole array. Only the numbers matter.
The condition means two things at once: every number from to must appear, and must not appear.
Count how many required values are missing. Also count how many times appears.
Changing an occurrence of into a missing required value is a two-for-one deal: it removes a bad and adds a missing number.
You need at least one operation per missing required number and at least one operation per occurrence of . Since one operation can fix one of each, the answer is .
To make , the array must satisfy exactly this:
Everything bigger than is irrelevant. It can sit there doing nothing. Very productive lifestyle.
What must be fixed?
Let:
Each missing number below must be created somehow, so we need at least operations.
Each occurrence of must be removed, because if appears, then the MEX cannot be . So we also need at least operations.
Therefore the answer is at least:
Now we need to show this lower bound is actually reachable.
Why the max is enough
Suppose a value below is missing and there is an occurrence of . Change that into the missing value.
That single operation fixes both problems:
So we pair as many missing values with occurrences of as possible.
If there are extra 's left afterward, change them to anything harmless, like or some value greater than .
If there are extra missing values left afterward, change some non-essential elements into them. This is always possible because the array has length and ; after keeping one copy of every already-present required value, there are enough other positions to modify.
So the exact minimum is:
Edge cases
If , there are no required smaller values. We only need to remove all zeroes, so the answer is just the number of zeroes.
If , then all values through must appear and must not appear. The same formula still works perfectly.
Algorithm
For each test case:
Complexity is per test case, and overall.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
int missing = 0;
for (int x = 0; x < k; x++) {
if (freq[x] == 0) missing++;
}
cout << max(missing, freq[k]) << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
int missing = 0;
for (int x = 0; x < k; x++) {
if (freq[x] == 0) missing++;
}
cout << max(missing, freq[k]) << '\n';
}
return 0;
}