Date: May 11, 2020
Location: Seattle, USA
Given a sorted array of size n, find the majority element. The majority element is the element that appears more than n/4 times. You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [1, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7]
Output: 3Example 2:
Input: [1, 1, 2, 2, 3, 3, 4, 5, 7, 7, 7]
Output: 7Example 3:
Input: [1, 1, 2, 3]
Output: 1Initialy, I've offer 2 solution
Both the solutions are O(N) time complexity. But, she was expecting the O(log n) solution and she given below HINT:
int windowSize = arr.length*(1/4)
a[0] == a[0+windowSize-1]
a[0] != a[0+windowSize-1]
I understood it was a binary search apporach, but couldn't make it happen and time is crossed.
Related problems: