Z13
Majority Element II
Problem Statement & Objective
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
Function Signature (Python)
majority_elements(nums: list[int]) -> list[int]Input & Output Contract
Output Rule:List of integers appearing more than n/3 times.
System Constraints
1 <= nums.length <= 5 * 10^4
-10^9 <= nums[i] <= 10^9Example Execution Walkthrough
nums = [3, 2, 3]
result: [3]
nums = [1, 2]
result: [1, 2]Example Test Scenarios (3)
Example 1
Input:
[[3,2,3]]Expected Output:
[3]Example 2: Single Element
Input:
[[1]]Expected Output:
[1]Example 3: İkisi de > 2/3
Input:
[[1,2]]Expected Output:
[1,2]