#C12135. Find Mode Height

    ID: 41529 Type: Default 1000ms 256MiB

Find Mode Height

Find Mode Height

You are given a list of heights. Your task is to determine the mode height in the list. The mode is defined as the height that occurs most frequently. In case there are multiple heights with the same maximum frequency, you must return the smallest height among them. Formally, if \( f(h) \) denotes the frequency of height \( h \) in the list and \( M = \max_{h} f(h) \), you must output:

\( \min\{ h \mid f(h) = M \} \)

If the list is empty, output the error message exactly as The list of heights cannot be empty.

Examples:

  • Input: [160, 170, 160, 165, 180, 165, 160] → Output: 160
  • Input: [150, 160, 170, 180] → Output: 150
  • Input: [150, 150, 160, 160, 170, 170, 180, 180] → Output: 150
  • Input: [] → Output: The list of heights cannot be empty
  • Input: [155] → Output: 155
  • Input: [150, 150, 150, 150, 150] → Output: 150

inputFormat

The input is read from stdin and has the following format:

n
h1 h2 h3 ... hn

Where n is an integer representing the number of heights, followed by a line containing n space-separated integers, each representing a height.

outputFormat

Output a single line to stdout containing the mode height as specified. If the input list is empty (i.e. n equals 0), output the error message: The list of heights cannot be empty.

## sample
7
160 170 160 165 180 165 160
160