Skip to content

Instantly share code, notes, and snippets.

@arkadyark
Created October 28, 2019 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkadyark/0cfb4c7da582f3e25acc231d420d33bf to your computer and use it in GitHub Desktop.
Save arkadyark/0cfb4c7da582f3e25acc231d420d33bf to your computer and use it in GitHub Desktop.
Missing Integers interview question
'''
Given a list of ordered integers with some of the numbers missing (and with possible duplicates), find the missing numbers.
Example:
> missingInts([1, 3, 3, 3, 5, 6])
> 2, 4
> missingInts([1, 2, 3, 4, 4, 7, 7])
> 5, 6
'''
def missingInts(l):
missing = []
prev = l[0]
for i in range(1, len(l)):
if l[i] == prev:
# Duplicates, just keep moving
continue
elif l[i] == prev - 1:
prev = l[i]
else:
missing += list(range(prev+1, l[i]))
prev = l[i]
return missing
if __name__ == '__main__':
print(missingInts([1, 3, 3, 3, 5, 6]))
print(missingInts([1, 2, 3, 4, 4, 7, 7]))
print(missingInts([2, 4, 7, 7]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment