public static int firstMissingPositive(int[] A) {//AC after consulting
        int len = A.length;
        int i = 0;
        while (i < len) {
            if (A[i] != i+1 && A[i] >= 1 && A[i] <= len && A[A[i]-1] != A[i]) {
                int temp = A[A[i]-1];
                A[A[i]-1] = A[i];
                A[i] = temp;
            }
            else {
                ++i;
            }
        }
        for (i = 0; i < len; ++i) {
            if (A[i] != i+1) return i+1;
        }
        return len+1;
    }