package problems;

public class FirstMissingPositive {
    public int firstMissingPositive(int[] A) {
        int n = A.length;
        if (n <= 0)
            return 1;

        for (int i = 0; i < n; i++) {
            if (A[i] > 0 && A[i] <= n)
                if (A[i] != i + 1 && A[A[i] - 1] != A[i]) {
                    int t = A[i] - 1;
                    A[i] = A[t];
                    A[t] = t + 1;
                    i--;
                }
        }
        for (int i = 0; i < n; i++)
            if (A[i] != i + 1)
                return i + 1;
        return n + 1;
    }

    public static void main(String[] args) {
        FirstMissingPositive fm = new FirstMissingPositive();
        int[] a = { 3, 4, -1, 1 };
        System.out.println(fm.firstMissingPositive(a));
    }
}
@@ -108,40 +108,34 @@
    i
-nt n = A.length;%0A        if (n %3C
+f (A == null %7C%7C A.length =
 = 0)
@@ -157,16 +157,24 @@
 turn 1;%0A
+        
 %0A       
@@ -194,22 +194,26 @@
  0; i %3C 
-n; i++
+A.length; 
 ) %7B%0A    
@@ -248,44 +248,16 @@
  %3C= 
-n)%0A                if (A%5Bi%5D != i + 1
+A.length
  && 
@@ -276,18 +276,16 @@
 != A%5Bi%5D)
- %7B
 %0A       
@@ -297,19 +297,18 @@
     
-    int t =
+swap(A, i,
  A%5Bi
@@ -312,16 +312,17 @@
 A%5Bi%5D - 1
+)
 ;%0A      
@@ -327,24 +327,29 @@
         
+else%0A
         
 A%5Bi%5D = A
@@ -344,19 +344,19 @@
     
-A%5Bi%5D = A%5Bt%5D
+        i++
 ;%0A  
@@ -357,32 +357,34 @@
 ++;%0A        
+%7D%0A
         
     A%5Bt%5D = t
@@ -375,196 +375,254 @@
     
-    A%5Bt%5D = t + 1;%0A                    i--;%0A                %7D%0A        %7D%0A        for (int i = 0; i %3C n; i++)
+for (int i = 0; i %3C A.length; i++) %7B%0A            if (A%5Bi%5D != i + 1)%0A                return i + 1;%0A        %7D%0A        return A.length + 1;%0A    %7D
 %0A    
+%0A
     
-    if (A%5Bi%5D != i + 1)%0A                return i + 1;%0A        return n + 1
+void swap(int%5B%5D A, int i, int j) %7B%0A        int t = A%5Bi%5D;%0A        A%5Bi%5D = A%5Bj%5D;%0A        A%5Bj%5D = t
 ;%0A  

Update First Missing Positive