Given an integer array nums sorted in non-decreasing order and an integer target, determine if the target is a majority element in the array.
A majority element is an element that appears more than nums.length / 2 times in the array. In other words, it must appear in more than half of the array positions.
Key insight: Since the array is already sorted, all occurrences of the target (if any) will be grouped together consecutively. This property allows us to use efficient searching algorithms instead of counting each element.
Goal: Return true if target appears more than n/2 times, false otherwise.
Input & Output
Visualization
Time & Space Complexity
Must examine every element once to count occurrences
Only uses a single counter variable
Constraints
- 1 โค nums.length โค 1000
- -109 โค nums[i], target โค 109
- nums is sorted in non-decreasing order