Skip to content

Commit e56488e

Browse files
authoredOct 20, 2021
Create 1150-check-if-a-number-is-majority-element-in-a-sorted-array.js
1 parent b3e5b26 commit e56488e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
function isMajorityElement(nums, target) {
7+
let firstIdx = bs(nums, target)
8+
let endIdx = firstIdx + (~~(nums.length / 2))
9+
if(endIdx < nums.length && nums[endIdx] === target) return true
10+
return false
11+
}
12+
13+
function bs(arr, target) {
14+
let l = 0, h = arr.length - 1
15+
while(l < h) {
16+
const mid = l + ((h - l) >> 1)
17+
if (arr[mid] < target) l = mid + 1
18+
else h = mid
19+
}
20+
return l
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.