#K4936. Majority Element Finder
Majority Element Finder
Majority Element Finder
Given an integer array of size \( n \), determine which elements, if any, appear more than \( \lfloor n/2 \rfloor \) times. In other words, find the element(s) whose count is strictly greater than \( n/2 \). If such element exists, output all of them in ascending order (separated by a space). Otherwise, output None
. Note that while typically only one element can satisfy this condition, your solution should be capable of handling multiple valid elements if they occur.
Example:
- For \( n = 7 \) and the array
[3, 3, 4, 2, 4, 4, 4]
, the output should be4
because 4 appears 4 times which is more than \( \lfloor 7/2 \rfloor = 3 \). - For \( n = 5 \) and the array
[3, 1, 3, 2, 4]
, there is no element appearing more than \( \lfloor 5/2 \rfloor = 2 \) times; hence, the output isNone
.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains a single integer \( n \) representing the number of elements in the array.
- The second line contains \( n \) space-separated integers.
If \( n = 0 \), the second line will be empty.
outputFormat
The output should be printed to stdout
as follows:
- If one or more majority elements are found, print them in ascending order separated by a single space.
- If no majority element exists, print
None
(without quotes).
7
3 3 4 2 4 4 4
4
</p>