The Silver Medal
HardAcc. 85.4%
+40 XP 20
The Second Best
Finding the maximum is easy; finding the runner-up requires tracking two leaders simultaneously.
The Assignment
Your function receives an array scores.
- Initialize
firstandsecondto a very small number (-Infinity). - Iterate through the array.
- If current >
first, updatesecond = firstandfirst = current. - Else if current >
secondand current !=first, updatesecond. - Print
second. If no such number exists, print -1.
01EXAMPLE 1
Input
[10, 5, 10, 8]Output
8Explanation: 10 is first, 8 is second.
Constraints
- Single pass (O(n)) only.
- Do not use .sort().
ArraysLogic
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
scores[10,5,8]
Expected Output
8
Click RUN to test your solution