Facebook | Phone | Find the Quarter Majority
Anonymous User
3113

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: 3

Example 2:

Input: [1, 1, 2, 2, 3, 3, 4, 5, 7, 7, 7]
Output: 7

Example 3:

Input: [1, 1, 2, 3]
Output: 1

Initialy, I've offer 2 solution

  1. Use Map with key and count pair and go through the Map and return the true, if any of the count >= 1/4*length;
  2. Use for loop with two pointer(first, prev) , increase the count, if values are same and return true when you see the count >=1/4* length;

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:

Comments (17)