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

# Count Subarrays Where Max Element Appears at

> Tested Python solution for LeetCode 2962 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2962, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/description/).

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

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

## Problem

You are given an integer array `nums` and a positive integer `k`.

Return the number of subarrays where the maximum element of `nums` appears at least `k` times in that subarray.

A subarray is a contiguous sequence of elements within an array.

### Examples

```
Input: nums = [1,3,2,3,3], k = 2
Output: 6
Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
```

```
Input: nums = [1,4,2,1], k = 3
Output: 0
Explanation: No subarray contains the element 4 at least 3 times.
```

### Constraints

* 1 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^6
* 1 \<= k \<= 10^5

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def count_subarrays(self, nums: list[int], k: int) -> int:
        mx = max(nums)
        total = 0
        count = 0
        left = 0
        for value in nums:
            if value == mx:
                count += 1
            while count >= k:
                if nums[left] == mx:
                    count -= 1
                left += 1
            total += left
        return total
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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