#1150
Easy Algorithms Check if a number is majority element in a sorted array
Array Binary Search
59.9% acceptance
Mar 31, 2026
480
36
Given an integer array nums sorted in non-decreasing order and an integer target, return true if target is a majority element, or false otherwise.
A majority element in an array nums is an element that appears more than nums.length / 2 times in the array.
Solution
Rust
Time O(log n)
Space O(1)
impl Solution {
pub fn is_majority_element(nums: Vec<i32>, target: i32) -> bool {
// Binary search for first and last occurrence
let first = nums.partition_point(|&x| x < target);
let last = nums.partition_point(|&x| x <= target);
last - first > nums.len() / 2
}
}