Created
January 5, 2018 17:22
-
-
Save folksilva/463fe1880ae2acb2936bea09d347fbfa to your computer and use it in GitHub Desktop.
Daily Coding Problem: Problem #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its O(nLogn)