Official

A - クラス委員長の選出 / Election of the Class President Editorial by admin

GPT 5.4 High

Overview

This is a problem where you need to check whether there is exactly one student with the maximum number of votes, and output that student’s number if there is exactly one, or \(-1\) if there are multiple such students.

Analysis

What this problem requires is not ranking who got how many votes, but rather knowing what the maximum number of votes is and how many people received that many votes.

For example, if the vote counts are

\([3, 5, 2, 5]\)

the maximum is \(5\), but since 2 students have that many votes, the answer is \(-1\).

On the other hand, if the vote counts are

\([3, 5, 2, 4]\)

the maximum is \(5\), and only student \(2\) has that many votes, so the answer is \(2\).

Key Insight

We only need the following 3 things:

  1. Find the maximum value \(mx\) of array \(A\)
  2. Count how many students have \(mx\) votes
  3. If there is exactly 1 such student, output their position

About the Naive Approach

For example, you could sort the vote counts and check the maximum, but sorting takes \(O(N \log N)\). Since we only need to know the maximum and its count for this problem, sorting is somewhat overkill.

Simply scanning through the array is sufficient, and it runs fast even for \(N \leq 2 \times 10^5\).

How to Solve It

In Python, the following built-in functions can be used:

  • max(a) : Find the maximum value
  • a.count(mx) : Count how many times the maximum value appears
  • a.index(mx) : Find the position where the maximum value first appears

If the maximum value appears exactly once, output the student number corresponding to that position. Note that index returns a 0-indexed position, so you need to add +1 to convert it to a student number.

Algorithm

  1. Read the input and create the array \(A\) of vote counts
  2. Find \(mx = \max(A)\)
  3. Count how many times \(mx\) appears in \(A\)
  4. If the count is 1, output the position of \(mx\) \(+1\)
  5. Otherwise, output \(-1\)

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

max, count, and index each scan the array once, so the total is \(O(3N)\). Ignoring constant factors, this is \(O(N)\).

Implementation Notes

  • Since student numbers start from \(1\), you need to add +1 to the result of a.index(mx).

  • If the maximum value appears multiple times, output -1 before checking the position.

  • The input is read all at once using sys.stdin.buffer.read(), which is fast even for large inputs.

    Source Code

import sys

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    n = data[0]
    a = data[1:1 + n]

    mx = max(a)
    if a.count(mx) == 1:
        print(a.index(mx) + 1)
    else:
        print(-1)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.

posted:
last update: