Codility 'MissingInteger' Solution

Martin Kysel · June 1, 2014

Short Problem Definition:

Find the minimal positive integer not occurring in a given sequence.

MissingInteger

Complexity:

expected worst-case time complexity is O(N)

expected worst-case space complexity is O(N)

Execution:

You only need to consider the first (N) positive integers. In this specification 0 does not count as a valid candidate! Any value that is below 1 or above N can be ignored.

Solution:

def solution(A):
    seen = [False] * len(A)
    for value in A:
        if 0 < value <= len(A):
            seen[value-1] = True

    for idx in xrange(len(seen)):
        if seen[idx] == False:
            return idx + 1

    return len(A)+1

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.