My issue
My code is not passing in some test cases . If anyone wondering why i pushed arr[0] after sorting it is to calculate the MEX until n ( when i hit the corner i will come out of the loop without cal the last subarray so pushed first element which definitetly smaller than the last element) . So could some one what is the wrong in my code
My code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// your code goes here
int t ;
cin >> t;
while ( t > 0){
int n;
cin >> n;
vector <int> arr(n);
for (int i = 0; i < n;i++)cin >> arr[i];
sort(arr.begin(),arr.end());
arr.push_back(arr[0]);
if (arr[0] != 0)cout << arr[0] - 1 << endl;
else {
int mex = 1;
int count = 1;
int k = 0;
for (int i = 0; i < n;i++){
if (arr[i] == arr[i+1])continue;
else if (arr[i] + 1 == arr[i+1]){
count ++ ;
}
else {
if (mex == 1)mex = count;
else {
if (mex == count)k++;
}
count = 1;
}
}
if (mex == 1)cout << -1 << endl;
else cout << k << endl;
}
t--;
}
return 0;
}
Problem Link: STBMEX Problem - CodeChef