Daily Coding Problem Solution 4


algorithms

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.


Attempt 1

def lowest_positive(arr):
    num = None
    arr = sorted(list(set(arr)))
    # arr = [i for i in arr if i > 0]
    for i, n in enumerate(arr):
        if n > 0: # if uncomment above, comment here
            if i+1 < len(arr):
                if n+1 == arr[i+1]:
                    continue
                else:
                    num = n+1
                    break
    if num is None:
        num = arr[-1] + 1
    return num

assert lowest_positive([3, 4, -1, 1]) == 2, 'fail'
assert lowest_positive([1, 2, 0]) == 3, 'fail'

nLog(n)

Attempt 2

def lowest_positive2(arr):
    # 2nd solution
    minimum = None
    nums = []

    for i, n in enumerate(arr):
        if n > 0:
            nums.append(n)
            if minimum is None:
                minimum = n
            if n < minimum:
                minimum = n

    i = 1
    while 1:
        if minimum+i in nums:
            i+=1
            continue
        else:
            return minimum+i

O(n^2) worst case

Attempt 3

def lowest_positive3(arr):
    # 3rd solution
    minimum = None
    maximum = None
    nums = []

    for i, n in enumerate(arr):
        if n > -1:
            nums.append(n)
            if minimum is None:
                minimum = n
            if n < minimum:
                minimum = n

            if maximum is None:
                maximum = n
            if n > maximum:
                maximum = n
    i = 1
    while 1:
        if minimum+i == nums[i-1]:

            i+=1
            continue
        else:
            return (minimum+i)

    return maximum+1

O(n)

Written by

Abdur-Rahmaan Janhangeer

Chef

Python author of 7+ years having worked for Python companies around the world

Suggested Posts

Daily Coding Problem #1: Two Sum Solution in Python

This problem was recently asked by Google and is a classic interview question often referred to as “...

Read article

Daily Coding Problem #2: Product of Array Except Self

This problem was asked by Uber and is a popular variation of array manipulation questions. The Probl...

Read article

Daily Coding Problem Solution 3

This problem was asked by Google. Given the root to a binary tree, implement serialize(root), which ...

Read article
Free Flask Course