B - MissingNo. Editorial by en_translator
We can sort \(A_1,\ldots,A_N\) to assume \(A_1<\ldots< A_N\) without changing the answer.
If two adjacent terms differ by two, the answer is between them. Since the Constraints guarantee that the answer is uniquely determined, such a position always occurs once.
The complexity of this problem is \(O(N\log N)\).
Sample code (Python)
N=int(input())
A=sorted(list(map(int,input().split())))
for i in range(N-1):
if A[i+1]-A[i]==2:
print(A[i]+1)
Sample code (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int>a(n);
for(auto&x:a)cin >> x;
sort(a.begin(),a.end());
for(int i=0;i<n-1;i++){
if(a[i+1]-a[i]==2){
cout << a[i]+1 << endl;
}
}
}
There is a trick that enables to solve it in a total of \(O(N)\) time, without sorting at all. Can you figure out how to do so?
(Hint: if you subtract the minimum value of \(A_i\) from the sequence, the range becomes from \(0\) to \(N\).)
posted:
last update: