Official

A - 山脈の最高峰 / The Highest Peak of the Mountain Range Editorial by admin Editorial for printing

Qwen3-Coder-480B

Overview

Given a set of mountain elevations, find the unique maximum value (strict maximum). If there are multiple occurrences of the maximum value, output \(0\).

Analysis

In this problem, an “isolated peak” refers to a mountain that is strictly higher than all other mountains, meaning the maximum elevation must exist exactly once.

Let’s look at the following example inputs.

Example 1:

Input:

5
1 3 2 5 4

The maximum value is \(5\), and it exists exactly once, so the answer is \(5\).

Example 2:

Input:

5
1 5 2 5 3

The maximum value is \(5\), but it appears twice, so no mountain can be said to be “higher than all others.” Therefore, the answer is \(0\).

Thus, the essence of the problem is determining “whether the maximum value is unique.”

A naive approach would be to compare every mountain against all others (nested loops), but since \(N\) can be up to \(10^6\), this would have a time complexity of \(O(N^2)\) and result in TLE (Time Limit Exceeded).

However, it is sufficient to simply find the maximum value in a single pass and count its occurrences. This greatly reduces the computational complexity.

Algorithm

  1. First, find the maximum value \(max\_height\) of the array \(A\).
  2. Next, count how many times that maximum value appears in the array.
  3. If the count is exactly 1, then it is the isolated peak, so output that value.
  4. Otherwise (i.e., if it appears 2 or more times), no isolated peak exists, so output \(0\).

Complexity

  • Time complexity: \(O(N)\)
    • Finding the maximum value and counting its occurrences each take \(O(N)\).
  • Space complexity: \(O(N)\)
    • \(O(N)\) is required to store the input array \(A\).

Implementation Notes

  • Use max(A) to obtain the maximum value.

  • Use A.count(max_height) to get the number of occurrences of that maximum value.

  • Branch based on whether the count is 1 or not.

    Source Code

N = int(input())
A = list(map(int, input().split()))

max_height = max(A)
if A.count(max_height) == 1:
    print(max_height)
else:
    print(0)

This editorial was generated by qwen3-coder-480b.

posted:
last update: