// File: zengfenfei[P]codility/src/MissingInteger.cpp
// Function: solution(std::vector<int, std::allocator<int>>&)
int solution(vector<int> &A) {
	int min = 0;	// Positive minimal
	for (int i = 0; i < A.size(); i++) {
		if (A[i] > 0 && (min == 0 || A[i] < min)) {
			min = A[i];
		}
	}
	if (min == 0 || min > 1) {
		return 1;
	}

	vector<int> possibleMissing(A.size(), 0);
	for (int i = 0; i < A.size(); i++) {
		int minOffset = A[i] - min - 1;
		if (minOffset >= 0 && minOffset < A.size()) {
			possibleMissing[minOffset]++;
		}
	}

	for (int i = 0; i < A.size(); i++) {
		if (possibleMissing[i] == 0) {
			return min + i + 1;
		}
	}
	return 0; 	// Will never run
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13d0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x1235
movq -0x10(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x13f0
cmpl $0x0, (%rax)
jle 0x1228
cmpl $0x0, -0x14(%rbp)
je 0x1216
movq -0x10(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x13f0
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jge 0x1228
movq -0x10(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x13f0
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x122a
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x11cd
cmpl $0x0, -0x14(%rbp)
je 0x1241
cmpl $0x1, -0x14(%rbp)
jle 0x124d
movl $0x1, -0x4(%rbp)
jmp 0x13a2
movq -0x10(%rbp), %rdi
callq 0x13d0
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
leaq -0x35(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x1410
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rcx
leaq -0x30(%rbp), %rdi
leaq -0x34(%rbp), %rdx
callq 0x1430
jmp 0x1285
leaq -0x35(%rbp), %rdi
callq 0x14b0
movl $0x0, -0x48(%rbp)
movslq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13d0
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x132d
movq -0x10(%rbp), %rdi
movslq -0x48(%rbp), %rsi
callq 0x13f0
movl (%rax), %eax
subl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jl 0x131d
movslq -0x4c(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13d0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x131d
movslq -0x4c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x13f0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x131d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x35(%rbp), %rdi
callq 0x14b0
jmp 0x13ae
jmp 0x131f
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1295
movl $0x0, -0x50(%rbp)
movslq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13d0
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jae 0x138b
movslq -0x50(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x13f0
cmpl $0x0, (%rax)
jne 0x137e
movl -0x14(%rbp), %eax
addl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x54(%rbp)
jmp 0x1399
jmp 0x1380
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1334
movl $0x0, -0x4(%rbp)
movl $0x1, -0x54(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x14d0
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x10a0
nopw (%rax,%rax)