You're given an integer array of size n, and your mission is to find all the truly dominant elements - those that appear more than ⌊n/3⌋ times.
This is a step up from the classic "Majority Element" problem where we looked for elements appearing more than ⌊n/2⌋ times. Here, we're looking for elements that appear in more than one-third of the array positions.
Key insight: There can be at most 2 elements that satisfy this condition in any array. Why? If three elements each appeared more than n/3 times, their total count would exceed n, which is impossible!
Example: In array [3,2,3] with n=3, we need elements appearing more than ⌊3/3⌋ = 1 time. Element 3 appears 2 times, so it's a majority element.
Input & Output
Constraints
- 1 ≤ nums.length ≤ 5 × 104
- -109 ≤ nums[i] ≤ 109
- Follow-up: Could you solve the problem in linear time and in O(1) space?