Problems > Third Distinct Maximum

Third Distinct Maximum

Difficulty: 1100 3000 ms 128 MB

implementation

Print the third largest distinct value; if fewer than three distinct values exist, print the maximum instead.

Input: the first line has N; the second line has N integers.

Output: the third distinct maximum, or the maximum.

Constraints:

  • 1 ≤ N ≤ 105
  • −109 ≤ ai ≤ 109

Sample Tests

Input 1
5
3 2 1 2 3
Output 1
1
Input 2
2
1 2
Output 2
2

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.