Given an integer array nums
sorted in non-decreasing order and an integer target
, return true
if target
is a majority element in nums
, or false
otherwise.
A majority element in an array nums
is an element that appears more than nums.length / 2
times in the array.
Example 1:
Input: nums = [2,4,5,5,5,5,5,6,6], target = 5
Output: true
Explanation:
The value 5 appears 5 times and the length of the array is 9.
Thus, 5 is a majority element because 5 > 9/2 is true.
Example 2:
Input: nums = [10,100,101,101], target = 101
Output: false
Explanation:
The value 101 appears 2 times and the length of the array is 4.
Thus, 101 is not a majority element because 2 > 4/2 is false.
Constraints:
1 <= nums.length <= 1000
1 <= nums[i], target <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem involves counting how many times the given number appears in the collection of numbers. We then check if that count is more than half the size of the collection. If it is, then it's a majority element, and if not, it isn't.
Here's how the algorithm would work step-by-step:
def is_majority_element_brute_force(numbers, target_number):
target_number_count = 0
for current_number in numbers:
if current_number == target_number:
target_number_count += 1
# Check if the number appears more than half the time
if target_number_count > len(numbers) / 2:
return True
# If we've made it here, it's not a majority
return False
The key to solving this efficiently is recognizing the input is sorted. Instead of checking every element, we leverage the sorted nature to quickly find the first and last occurrences of our target number, allowing us to count its occurrences efficiently.
Here's how the algorithm would work step-by-step:
def is_majority_element(numbers, target):
array_length = len(numbers)
first_occurrence = find_first_occurrence(numbers, target)
if first_occurrence == -1:
return False
if numbers[first_occurrence + array_length // 2] == target:
# Check if element at majority index equals target.
return True
return False
def find_first_occurrence(numbers, target):
left_index = 0
right_index = len(numbers) - 1
first_occurrence_index = -1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
if numbers[middle_index] == target:
# Potential first occurrence, narrow search to the left
first_occurrence_index = middle_index
right_index = middle_index - 1
elif numbers[middle_index] < target:
left_index = middle_index + 1
else:
right_index = middle_index - 1
return first_occurrence_index
Case | How to Handle |
---|---|
Empty or null input array | Return false immediately since there are no elements to satisfy the majority condition. |
Array with one element and target matches that element | Return true since the target appears once, and length/2 is 0, so 1 > 0. |
Array with one element and target does not match | Return false, since the target is not present and cannot be a majority. |
Target is smaller than the smallest element in the array | The number of occurrences will be zero; hence, return false. |
Target is larger than the largest element in the array | The number of occurrences will be zero; hence, return false. |
Target appears exactly n/2 times (where n is the array length) | Return false since it needs to be strictly *more* than n/2. |
Large array size (potential integer overflow in calculations of n/2) | Use long or handle the division with appropriate data types to avoid overflow. |
Target is present at the beginning of the array | The algorithm should still correctly calculate the frequency regardless of where the target appears. |