Given a list of numbers, write a program to print the smallest positive integer missing in the given numbers.
# A function that takes a list of numbers and return that the smallest missing positive integer
def missingPostiveInteger(listA):
maximum = max(listA) # Storing the largest value
if maximum < 1:
# if all the numbers in the list are negative
return 1
if len(listA) == 1:
# In case the list contains one number
return 2 if listA[0] == 1 else 1
le = [0] * maximum
for number in range(len(listA)):
if listA[number] > 0:
if le[listA[number] - 1] != 1:
le[listA[number] - 1] = 1
for number in range(len(le)):
if le[number] == 0:
return number + 1
return number + 2
# Testing Code
A = [0, 20, 40, -10,1,2,3,5 -20]
print(missingPostiveInteger(A))
Comments
Leave a comment