Answer to Question #231262 in Python for bhuvana

Question #231262

Given a list of numbers, write a program to print the smallest positive integer missing in the given numbers.


1
Expert's answer
2021-09-04T07:35:54-0400
# 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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS