> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Check If a Number Is Majority Element in a

> Tested Python solution for LeetCode 1150 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 1150, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1150   # by problem number
lcpy gen -s check_if_a_number_is_majority_element_in_a_sorted_array   # by problem name
```

## 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.

### Examples

```
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.

```
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 <= 10^9`
* `nums` is sorted in non-decreasing order.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_a_number_is_majority_element_in_a_sorted_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/check_if_a_number_is_majority_element_in_a_sorted_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n)
    # Space: O(1)
    def is_majority_element(self, nums: list[int], target: int) -> bool:
        left = 0
        right = len(nums)
        while left < right:
            mid = (left + right) // 2
            if nums[mid] < target:
                left = mid + 1
            else:
                right = mid
        candidate = left + len(nums) // 2
        return candidate < len(nums) and nums[candidate] == target
```

## Complexity

| Time | Space |
| - | - |
| O(log n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.