-
Notifications
You must be signed in to change notification settings - Fork 1k
/
findingLeastPositiveNumber.py
32 lines (28 loc) · 1.06 KB
/
findingLeastPositiveNumber.py
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
26
27
28
29
30
31
32
# finding least positive number
# 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.
# code contributed by devanshi katyal
# space complexity:O(1)
# time complexity:O(n)
def MainFunction(arr, size):
for i in range(size):
if (abs(arr[i]) - 1 < size and arr[abs(arr[i]) - 1] > 0):
arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]
for i in range(size):
if (arr[i] > 0):
return i + 1
return size + 1
def findpositive(arr, size):
j = 0
for i in range(size):
if (arr[i] <= 0):
arr[i], arr[j] = arr[j], arr[i]
j += 1
return MainFunction(arr[j:], size - j)
arr = list(map(int, input().split(" ")))
# sample input= [-1, 2, 4,3 , 0]
print("the smallest missing positive number is", findpositive(arr, len(arr)))
# output: the smallest missing positive number is 1