← DiffPush

Second largest element in array

Baseline Arrays · Easy O(n) · O(1)

Runner-Up Power Draw

A datacenter ranks rack power draw to plan failover capacity. The top rack is already handled by a dedicated circuit, so the facility team needs the second highest distinct draw across the fleet. If every rack draws the same power there is no runner-up, and the answer is -1.

Input: An integer n followed by an array Arr of n integer power draws.

Output: The second largest distinct value in Arr, or -1 when no such value exists.

Constraints

Examples

Example 1

Input: {"n":6,"Arr":[12,35,1,10,34,1]}
Output: 34
35 is the largest distinct draw and 34 is the next one down, so the runner-up is 34.

Example 2

Input: {"n":5,"Arr":[2,4,6,8,10]}
Output: 8
The draws climb monotonically, so the runner-up keeps shifting forward as 10 passes it.

Solve this in your browser →

Also on LeetCode ↗