Back to DSA sheet

Majority Element II

MediumArrays
Open on LeetCodeAmazonGoogle

Given an array of size n, return every element that appears more than n/3 times. There are at most two such values.

Input format (stdin): the first line has n. The second line has n integers. Output the qualifying values in ascending order, space separated (blank line if none).

Examples
Input: 3 3 2 3
Output: 3
3 appears twice, more than 3/3=1.
Input: 1 1
Output: 1
Constraints
  • 1 <= n <= 5*10^4
  • -10^9 <= nums[i] <= 10^9
Sheets
Striver A2Z
majority-element-ii.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.