#K8856. Second Largest Element Finder

    ID: 37335 Type: Default 1000ms 256MiB

Second Largest Element Finder

Second Largest Element Finder

Given an array of integers, find the second largest element in the array. The second largest element is defined as the largest element strictly less than the maximum element. If no such element exists (for example, when the array contains only one element or all elements are identical), output -1.

Consider the example: for the array [10, 5, 20, 4, 8], the largest element is 20 and the second largest is 10.

For a more formal description, let \( A = [a_1, a_2, \dots, a_n] \) be the array. You need to determine the value:

\[ \max\{ x : x < \max_{1 \leq i \leq n}a_i,\ x \in A \}\]

If no such \( x \) exists, output \( -1 \).

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains a single integer \( n \) representing the number of elements in the array.
  2. The second line contains \( n \) space-separated integers representing the elements of the array.

outputFormat

Output a single integer to standard output (stdout): the second largest element in the array. If no such element exists, output -1.

## sample
5
10 5 20 4 8
10

</p>