A silent auction records every bid placed on an item as it comes in. Multiple bidders can end up placing the exact same amount, so the recorded list of bids may contain repeats. The auction house wants to announce the third-highest DISTINCT bid amount, to recognize the "bronze tier" bidder. If the recorded bids only produced one or two distinct amounts in total, announce the highest bid amount instead.
n, the number of recorded bids.n space-separated integers, the bid amounts in the order they were recorded. Amounts may repeat and may be any 32-bit signed integer (a negative "bid" simply represents a value below the auction's reference point).Print a single integer: the third-highest distinct bid amount, or the highest bid amount if fewer than three distinct amounts were recorded.
1 <= n <= 10^4-2^31 <= bid[i] <= 2^31 - 1Example 1
Input
3 3 2 1
Expected
1
Explanation
The three bids are all distinct: 3, 2, and 1. Sorted from highest to lowest they are 3, 2, 1, so the third-highest distinct bid is 1.
Example 2
Input
2 1 2
Expected
2
Explanation
Only two distinct bid amounts were ever recorded (1 and 2), which is fewer than three, so the auction house instead announces the highest bid, which is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →