A. Special Week With Positive Integers
Description
Please notice the shortage of the Memory limit!
Special Week is now just wondering the minimum positive integer which is not in the integer sequence. For example, if the sequence is [2,4,3,4] then the answer is obviously 1, if the sequence is [1,2,3,4] then the answer is obviously 5.
Please notice that there also are some negative integers in the sequence.
So, just given Special Week one integer sequence of nn integers. Please help her to find the minimum positive integer without the sequence.
Input
The first line contains one positive integer n(1≤n≤10 6 ), indicating the length of the sequence.
The second line contains nn integers a[i](−10 ^18 ≤a[i]≤10 ^18 ), the element in the sequence.
Output
One integer, the minimum positive integer without the sequence.
Examples
input
4
2 4 3 4
output
1
input
4
1 2 3 4
output
5
input
4
-1 -2 -3 -4
output
1
AC code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int LL;
const int N=1e6+10;
LL q[N];
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) cin>>q[i];
sort(q,q+n);
if(q[n-1] <1) {
printf("1");
return 0;
}
else if(q[0] >1){
printf("1");
return 0;
}
else{
int i;
for(i=0;i<n;i++){
if(q[i]<1) continue;
else{
if(q[i]>1 && q[i-1] <1){
printf("1");
return 0;
}
if(q[i]+1 != q[i+1]){
cout<<q[i]+1<<endl;
return 0;
}
}
}
if(i == n) cout<<q[i-1]+1<<endl;
}
return 0;
}