#K56162. Second Largest Number

    ID: 30138 Type: Default 1000ms 256MiB

Second Largest Number

Second Largest Number

Given a list of integers, your task is to find the second largest distinct number in the list. If there are fewer than two distinct integers, output NaN.

The solution should follow these guidelines:

  • If the input list has at least two distinct integers, print the second largest one.
  • If the list has fewer than two distinct integers (including an empty list or a list with one element), print NaN.

Note: If there is a tie for the largest value, the second largest must be the next distinct lower number.

The algorithm can be described in LaTeX as follows:

$$\text{Let } first = -\infty,\ second = -\infty.\\ \text{For each } x \text{ in the array:}\\ \quad \textbf{if } x > first:\quad second = first,\ first = x.\\ \quad \textbf{elif } first > x > second:\quad second = x.\\ \text{If } second = -\infty, \text{ then output } \texttt{NaN}; \text{ otherwise, output } second. $$

inputFormat

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

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

outputFormat

Output a single line to stdout containing the second largest distinct number. If there is no valid second largest number, output NaN (case sensitive).

## sample
5
1 3 4 4 2
3