Best and very simple C++ solution (Must check)


#1

int Solution::firstMissingPositive(vector &A)
{
int n=A.size();
int temp[n+1]={0}; //new temp array with size of the max() value of our given array, all
//initalized to 0
for(int i=0;i<n;i++)
{
if(A[i] > 0 && A[i] <= n)
temp[A[i]] = 1;
}
for (int i = 1; i <= n; i++)
{
if (!temp[i])
return i; // Find the first element which didn’t appear in the original array
}
return n+1; //If the original array was of the type {1, 2, 3} in its sorted form
}