0 out of 464 challenges solved
Write a function `is_majority` that determines if a given element is the majority element in a sorted array. The majority element is defined as the element that appears more than `n/2` times in the array, where `n` is the length of the array. #### Function Signature ```python [main.nopy] def is_majority(arr: list[int], x: int) -> bool: ``` #### Input - `arr`: A sorted list of integers. - `x`: An integer, the element to check for majority. #### Output - Returns `True` if `x` is the majority element, otherwise `False`. #### Example Usage ```python [main.nopy] is_majority([1, 2, 3, 3, 3, 3, 10], 3) # Output: True is_majority([1, 1, 2, 4, 4, 4, 6, 6], 4) # Output: False is_majority([1, 1, 1, 2, 2], 1) # Output: True is_majority([1, 1, 2, 2], 4, 1) # Output: False ``` #### Constraints - The array is sorted in non-decreasing order. - The function should use binary search for efficiency.
def is_majority(arr: list[int], x: int) -> bool: """ Determine if x is the majority element in the sorted array arr. Args: arr (list[int]): The sorted array of integers. x (int): The element to check for majority. Returns: bool: True if x is the majority element, False otherwise. """ # Placeholder for the solution pass