Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created April 19, 2020 13:16
Show Gist options
  • Save jatinsharrma/a32fad36140204f72a84733696271a8f to your computer and use it in GitHub Desktop.
Save jatinsharrma/a32fad36140204f72a84733696271a8f to your computer and use it in GitHub Desktop.
Find Three Largest Numbers
#---------------------------------------------------------
#---------Three Largest Numbers----------
#----------------------------------------------------------
# this function returns three largest numbers
# complexity O(2n) = O(n) where n is length of array
def threeLargestNumbers(numbers):
if len(numbers) < 4:
return numbers
largest = [float("-inf")]*3
for i in numbers :
small = 0
for j in range(1,3):
if largest[j] < largest[small]:
small = j
if largest[small] < i:
largest[small] = i
return largest
print(threeLargestNumbers([9,2,3,4,5,6,7,8]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment