public class Solution {

public int firstMissingPositive(int[] A) {

int i = 0;

int n= A.length;

while(i < A.length){

if(A[i] == i+1 || A[i] <= 0 || A[i] > A.length)

i++;

else if(A[A[i]-1] != A[i])

swap(A, i, A[i]-1);

else i++;

}

for(int j=0;j<A.length;j++){

if(A[j]!=(j+1)){

return j+1;

}

}

return n+1;

}

private void swap(int[] A, int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}

}