Last active
October 17, 2019 22:17
-
-
Save shiv043/b4eb37e437e7de929f9f471f73721dd6 to your computer and use it in GitHub Desktop.
Assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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