511. Check If a Number Is a Majority Element in a Sorted Array

MediumBinary SearchBinary SearchArray

Given a sorted array and a target value, determine whether the target is a majority element — that is, whether it appears more than n/2 times (n being the array length). Use binary search to find its first and last positions. Return true or false. Input: '[arr], target'.

Input: '[arr], target'.

Output: Boolean — true or false.

Examples

Example 1
Input: [2,4,5,5,5,5,5,6,6], 5
Output: true
Explanation: 5 appears 5 times out of 9.
Example 2
Input: [10,100,101,101], 101
Output: false
Explanation: 101 appears 2 times of 4, not a majority.
Example 3
Input: [1], 1
Output: true
Explanation: Single matching element.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →