educative.io

Append a None to the end of the list and then perform Cyclic Sort

I append “None” to the end of the list and perform Cyclic sort. If “None” gets bubbled into the current position I’m on or if the number that gets bubbled in matches the index.

At the end, I go through the list from start to finish and find the index of the None and return that index.

def find_missing_number(nums):

  # TODO: Write your code here

  nums.append(None)

  i = 0

  while i < len(nums):

    j = nums[i]

    if j is not None:

      if i != j:

        nums[i],nums[j] = nums[j], nums[i]

      else:

        i += 1

    else:

      i += 1

  for i in range(len(nums)):

    if nums[i] is None:

      return i

        

  return -1