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
Check if Target 5 is Majority Element245555566Array length = 9, Target = 5Count of target 5 = 5 occurrencesMajority threshold = 9/2 = 45 > 4, so return true
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
Asked in
Google 15 Microsoft 12 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen