Loading...
Loading...
A class teacher has the test scores of n students. Print the second highest different score. If all students have the same score, there is no second highest score, so print -1.
-1 if it does not exist.Example 1
Input
5 70 85 92 85 60
Output
85
92 is the highest. The next different score is 85.
Example 2
Input
3 50 50 50
Output
-1
Every score is the same, so there is no second highest.
Try the problem first. Open one hint at a time.
You do not need to sort. Keep two variables, first and second, and update them in one pass.
A new score larger than first pushes the old first down to second.
A score equal to first must not become second, because the problem asks for a different score.
Start second at -1: if it never changes, -1 is already the right answer.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor