Problem Description
Given a sorted integer array and a target value, determine if the target is a majority element in the array. A majority element is an element that appears more than ⌊n/2⌋ times in the array, where n is the length of the array.
Key Insights
- The array is sorted, which allows efficient location of the target using binary search.
- Instead of counting all occurrences, we can find the leftmost index of the target and check the element at index (leftIndex + n/2).
- If the target is truly a majority element, the element at that calculated index will also be the target.
Space and Time Complexity
Time Complexity: O(log n), due to using binary search.
Space Complexity: O(1), as only a constant amount of extra space is used.
Solution
We first perform a binary search to locate the leftmost occurrence of the target in the sorted array. Once the leftmost index is found, we compute the index at leftIndex + n/2. If this index is within bounds and its element is equal to the target, then target appears more than n/2 times in the array, qualifying it as a majority element. This method leverages the sorted property to avoid a full count, resulting in an efficient solution.