Efficient way to find missing elements in an integer sequence

前端 未结 16 1358
鱼传尺愫
鱼传尺愫 2020-12-01 04:32

Suppose we have two items missing in a sequence of consecutive integers and the missing elements lie between the first and last elements. I did write a code that does accomp

16条回答
  •  眼角桃花
    2020-12-01 05:12

    
     a=[1,2,3,7,5,11,20]
     b=[]
     def miss(a,b):
         for x in range (a[0],a[-1]):
            if x not in a:
                b.append(x)
         return b
     print (miss(a,b))
    
    

    ANS:[4, 6, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19]

    works for sorted,unsorted , with duplicates too

提交回复
热议问题