Input: nums =[2,4,5,5,5,5,5,6,6], target =5Output: trueExplanation: The value 5 appears 5 times and the length of the array is9.Thus,5is a majority element because 5>9/2istrue.
Example 2:
1
2
3
4
Input: nums =[10,100,101,101], target =101Output: falseExplanation: The value 101 appears 2 times and the length of the array is4.Thus,101is not a majority element because 2>4/2isfalse.
Method 1 – Binary Search for First and Last Occurrence#
Intuition:
Since the array is sorted, we can use binary search to efficiently find the first and last occurrence of the target. The count of the target is then last - first + 1. If this count is more than half the array’s length, the target is a majority element.
Approach:
Use binary search to find the first occurrence of the target.
Use binary search to find the last occurrence of the target.
classSolution {
public:bool isMajorityElement(vector<int>& nums, int target) {
int n = nums.size();
int first = findFirst(nums, target);
if (first ==-1) return false;
int last = findLast(nums, target);
return (last - first +1) > n /2;
}
intfindFirst(vector<int>& nums, int target) {
int l =0, r = nums.size() -1, ans =-1;
while (l <= r) {
int m = l + (r - l) /2;
if (nums[m] >= target) r = m -1;
else l = m +1;
if (nums[m] == target) ans = m;
}
return ans;
}
intfindLast(vector<int>& nums, int target) {
int l =0, r = nums.size() -1, ans =-1;
while (l <= r) {
int m = l + (r - l) /2;
if (nums[m] <= target) l = m +1;
else r = m -1;
if (nums[m] == target) ans = m;
}
return ans;
}
};
classSolution {
publicbooleanisMajorityElement(int[] nums, int target) {
int n = nums.length;
int first = findFirst(nums, target);
if (first ==-1) returnfalse;
int last = findLast(nums, target);
return (last - first + 1) > n / 2;
}
privateintfindFirst(int[] nums, int target) {
int l = 0, r = nums.length- 1, ans =-1;
while (l <= r) {
int m = l + (r - l) / 2;
if (nums[m]>= target) r = m - 1;
else l = m + 1;
if (nums[m]== target) ans = m;
}
return ans;
}
privateintfindLast(int[] nums, int target) {
int l = 0, r = nums.length- 1, ans =-1;
while (l <= r) {
int m = l + (r - l) / 2;
if (nums[m]<= target) l = m + 1;
else r = m - 1;
if (nums[m]== target) ans = m;
}
return ans;
}
}
classSolution {
funisMajorityElement(nums: IntArray, target: Int): Boolean {
funfindFirst(nums: IntArray, target: Int): Int {
var l = 0; var r = nums.size - 1; var ans = -1while (l <= r) {
val m = l + (r - l) / 2if (nums[m] >= target) r = m - 1else l = m + 1if (nums[m] == target) ans = m
}
return ans
}
funfindLast(nums: IntArray, target: Int): Int {
var l = 0; var r = nums.size - 1; var ans = -1while (l <= r) {
val m = l + (r - l) / 2if (nums[m] <= target) l = m + 1else r = m - 1if (nums[m] == target) ans = m
}
return ans
}
val n = nums.size
val first = findFirst(nums, target)
if (first == -1) returnfalseval last = findLast(nums, target)
return (last - first + 1) > n / 2 }
}
classSolution:
defisMajorityElement(self, nums: list[int], target: int) -> bool:
deffind_first(nums: list[int], target: int) -> int:
l, r, ans =0, len(nums) -1, -1while l <= r:
m = (l + r) //2if nums[m] >= target:
r = m -1else:
l = m +1if nums[m] == target:
ans = m
return ans
deffind_last(nums: list[int], target: int) -> int:
l, r, ans =0, len(nums) -1, -1while l <= r:
m = (l + r) //2if nums[m] <= target:
l = m +1else:
r = m -1if nums[m] == target:
ans = m
return ans
n = len(nums)
first = find_first(nums, target)
if first ==-1:
returnFalse last = find_last(nums, target)
return (last - first +1) > n //2
impl Solution {
pubfnis_majority_element(nums: Vec<i32>, target: i32) -> bool {
fnfind_first(nums: &Vec<i32>, target: i32) -> i32 {
let (mut l, mut r, mut ans) = (0, nums.len() asi32-1, -1);
while l <= r {
let m = l + (r - l) /2;
if nums[m asusize] >= target {
r = m -1;
} else {
l = m +1;
}
if nums[m asusize] == target {
ans = m;
}
}
ans
}
fnfind_last(nums: &Vec<i32>, target: i32) -> i32 {
let (mut l, mut r, mut ans) = (0, nums.len() asi32-1, -1);
while l <= r {
let m = l + (r - l) /2;
if nums[m asusize] <= target {
l = m +1;
} else {
r = m -1;
}
if nums[m asusize] == target {
ans = m;
}
}
ans
}
let n = nums.len() asi32;
let first = find_first(&nums, target);
if first ==-1 {
returnfalse;
}
let last = find_last(&nums, target);
(last - first +1) > n /2 }
}