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.
Adding the same to every element does not change the gaps between values. So stop staring at absolute positions; they are mostly a distraction.
To make the MEX at least , the shifted array must contain every value . What did those values look like before adding ?
Before the shift, those required values must have been : a block of consecutive integers.
Duplicates are useless here. Having five copies of does not help create . Convert the array mentally into a set of distinct values.
The answer is exactly the length of the longest consecutive run among the distinct array values. Sort, skip duplicates, and count adjacent differences of .
The operation adds one integer to every element. That means the whole array slides left or right on the number line, but its internal structure stays the same. Distances between values do not change.
Suppose after shifting, the MEX is at least . Then the shifted array must contain
If an original value becomes , then , so . Therefore, before the shift, the array must have contained
That is just a consecutive block of length in the original array.
So any achievable MEX requires the original array to contain consecutive distinct integers. Duplicates do not matter at all. They are dead weight for this problem.
Now go the other way. If the original array contains a consecutive block
then choose
After shifting, those values become
If this block is maximal, then is not present, so is not present after the shift. Thus the MEX is exactly .
Therefore, the maximum possible MEX is exactly:
Implementation is straightforward:
Edge cases:
long long keeps things clean.Complexity per test case:
for sorting, and
extra memory besides the array. Given the total , this is comfortably inside the limits. Like, not even sweating.
#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;
cin >> n;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
int ans = 1, cur = 1;
ll prev = a[0];
for (int i = 1; i < n; i++) {
if (a[i] == prev) continue;
if (a[i] == prev + 1) {
cur++;
} else {
cur = 1;
}
prev = a[i];
ans = max(ans, cur);
}
cout << ans << '\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;
cin >> n;
vector<ll> a(n);
for (ll &x : a) cin >> x;
sort(a.begin(), a.end());
int ans = 1, cur = 1;
ll prev = a[0];
for (int i = 1; i < n; i++) {
if (a[i] == prev) continue;
if (a[i] == prev + 1) {
cur++;
} else {
cur = 1;
}
prev = a[i];
ans = max(ans, cur);
}
cout << ans << '\n';
}
return 0;
}