-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2.py
More file actions
25 lines (18 loc) · 850 Bytes
/
Copy path2.py
File metadata and controls
25 lines (18 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
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.
"""
def missing_positive(iterable):
iterable.sort()
for i, num in enumerate(iterable):
if num < 0:
continue
if len(iterable) <= i and num != iterable[i+1]-1:
return num+1
else:
return "No missing number"
if __name__ == "__main__":
iterable = [int(i.strip()) for i in input("Enter the number seperated by commas: ").split(",")]
print(missing_positive(iterable))