Check If a Number Is Majority Element in a Sorted Array - Problem
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.
Input & Output
Example 1 — Target is Majority
$
Input:
nums = [2,4,5,5,5,5,5,6,6], target = 5
›
Output:
true
💡 Note:
Target 5 appears 5 times in array of length 9. Since 5 > 9/2 (which is 4), target 5 is a majority element.
Example 2 — Target is Not Majority
$
Input:
nums = [10,100,101,101], target = 101
›
Output:
false
💡 Note:
Target 101 appears 2 times in array of length 4. Since 2 ≤ 4/2 (which is 2), target 101 is not a majority element.
Example 3 — Target Not Found
$
Input:
nums = [1,1,2,2], target = 3
›
Output:
false
💡 Note:
Target 3 does not appear in the array, so it cannot be a majority element.
Constraints
- 1 ≤ nums.length ≤ 1000
- -109 ≤ nums[i], target ≤ 109
- nums is sorted in non-decreasing order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted array and target value
2
Count
Find occurrences of target
3
Check
Return count > length/2
Key Takeaway
🎯 Key Insight: Use binary search on sorted arrays to find element bounds in O(log n) time
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code