binary search left bound and right bound
class Solution {
public boolean isMajorityElement(int[] nums, int target) {
// search left
int left = 0, right = nums.length - 1;
while(left <= right){
int pivot = left + (right - left) / 2;
if(nums[pivot] >= target){
// we shrink to left
right = pivot - 1;
}else{
// it is too small
left = pivot + 1;
}
}
int p1 = left;
left = 0; right = nums.length - 1;
// find right boundry
while(left <= right){
int pivot = left + (right - left) / 2;
if(nums[pivot] > target){
// we shrink to left
right = pivot - 1;
}else{
// it is too small
left = pivot + 1;
}
}
int p2 = right;
return p2 - p1 + 1 > nums.length /2;
}
}
