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. This question was asked by Stri
Here's another python implementation with o(n) time complexity and o(1) space complexity
def segregate(arr):
length = len(arr)
neg_index = length
for i, value in enumerate(arr):
if(value < 1 and neg_index == length):
neg_index = i
if(neg_index != length and value >= 1):
temp = arr[i]
arr[i] = arr[neg_index]
arr[neg_index] = temp
neg_index += 1
return arr[:neg_index]
def missingPositiveNumber(arr):
arr = segregate(arr)
length = len(arr)
for i, value in enumerate(arr):
if(value - 1 < l):
arr[abs(value) - 1] = -(abs(arr[abs(value) - 1]))
for i, value in enumerate(arr):
if(value > 0):
return i + 1
return length + 1
print(missingPositiveNumber([1, -1, 2, 3]))