Skip to content

Instantly share code, notes, and snippets.

@shiv043
Last active October 17, 2019 22:17
Show Gist options
  • Select an option

  • Save shiv043/b4eb37e437e7de929f9f471f73721dd6 to your computer and use it in GitHub Desktop.

Select an option

Save shiv043/b4eb37e437e7de929f9f471f73721dd6 to your computer and use it in GitHub Desktop.
Assignment
"""
Logic:
Is to keep track of first maximum and
second maximum
"""
def max_second_num_from_arr(arr):
if len(arr) < 2:
return "-1"
first_max = max(arr[0], arr[1])
second_max = min(arr[0], arr[1])
for i in range(2, len(arr)):
if arr[i] > first_max:
second_max = first_max
first_max = arr[i]
else:
if arr[i] > second_max:
second_max = arr[i]
if first_max == second_max:
second_max = "-1"
return second_max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment