446. Find N/3 Majority Element Using Binary Search

EasyBinary SearchArrayBinary Search

Given an integer array nums, return the element that appears more than ⌊n/3⌋ times. It is guaranteed that such an element exists. If multiple majority elements exist, return the smallest one. Solve using binary search on sorted array.

Input: An integer array nums (guaranteed to have at least one majority element).

Output: The smallest element appearing more than n/3 times.

Examples

Example 1
Input: [3,2,3]
Output: 3
Explanation: 3 appears 2 times > 3/3=1. Majority is 3.
Example 2
Input: [1,2,1,2,3,1]
Output: 1
Explanation: 1 appears 3 times > 6/3=2. Majority is 1.
Example 3
Input: [5,5,5,5,1,2,3,4]
Output: 5
Explanation: 5 appears 4 times > 8/3=2.6. Majority is 5.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →