Skip to content

Instantly share code, notes, and snippets.

@shaddyshad
Last active January 13, 2021 16:24
Show Gist options
  • Save shaddyshad/b7dbcaa48619079631cc3ba760643e19 to your computer and use it in GitHub Desktop.
Save shaddyshad/b7dbcaa48619079631cc3ba760643e19 to your computer and use it in GitHub Desktop.
def solution(A):
# sort the array of unique items
sorted_a = list(sorted(set(A)))
# record the index of the last negative number or 0
neg_index = 0
for i in range(len(sorted_a)):
if sorted_a[i] <= 0 :
neg_index = i
# if no negatives, start loop at 0
if neg_index == 0:
return find_next(sorted_a[1:])
# if neg_index == len(arr) - meaning that all items are negative, return 1
if neg_index == len(sorted_a):
return 1
# else split the array from where positives starts
split_arr = sorted_a[neg_index+1: ]
return find_next(split_arr)
def find_next(arr):
if len(arr) == 0:
return 1
i = 1
while i <= len(arr):
curr = arr[i-1]
if curr != i:
return i
next_item = curr
if i + 1 < len(arr):
next_item = arr[i]
# if they refer to the same item, then the highest is the next item
if curr == next_item:
return curr + 1
# if not, check that next-curr == 1 if > 1, then curr+1,
if next_item - curr > 1:
return curr + 1
i += 1
if __name__ == "__main__":
a = [0,1,2,3,4,5,6,7,8,9,10,11,12]
n = solution(a)
print(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment