Skip to content

Instantly share code, notes, and snippets.

@folksilva
Created January 5, 2018 17:22
Show Gist options
  • Save folksilva/463fe1880ae2acb2936bea09d347fbfa to your computer and use it in GitHub Desktop.
Save folksilva/463fe1880ae2acb2936bea09d347fbfa to your computer and use it in GitHub Desktop.
Daily Coding Problem: Problem #4
"""
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.
https://dailycodingproblem.com/
"""
if __name__ == '__main__':
# Read input, numbers separated by commas
numbers = [int(n) for n in input().split(',')]
numbers.sort()
next_number = 1
for n in numbers:
if n > 0:
if n > next_number:
break
else:
next_number = n + 1
print(next_number)
@pixambu
Copy link

pixambu commented Mar 7, 2019

Its O(nLogn)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment