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

# Frequency of the Most Frequent Element

> Tested Python solution for LeetCode 1838 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1838, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Greedy](/catalog/topics/greedy), [Sliding Window](/catalog/topics/sliding-window), [Sorting](/catalog/topics/sorting), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/frequency-of-the-most-frequent-element/description/).

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

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

## Problem

The **frequency** of an element is the number of times it occurs in an array.

You are given an integer array `nums` and an integer `k`. In one operation, you can choose an index of `nums` and increment the element at that index by `1`.

Return *the* ***maximum possible frequency*** *of an element after performing* ***at most*** `k` *operations*.

### Examples

```
Input: nums = [1,2,4], k = 5
Output: 3
```

**Explanation:** Increment the first element three times and the second element two times to make nums = \[4,4,4].
4 has a frequency of 3.

```
Input: nums = [1,4,8,13], k = 5
Output: 2
```

**Explanation:** There are multiple optimal solutions:

* Increment the first element three times to make nums = \[4,4,8,13]. 4 has a frequency of 2.
* Increment the second element four times to make nums = \[1,8,8,13]. 8 has a frequency of 2.
* Increment the third element five times to make nums = \[1,4,13,13]. 13 has a frequency of 2.

```
Input: nums = [3,9,6], k = 2
Output: 1
```

### Constraints

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

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n) for the sort, O(n) for the sliding window
    # Space: O(1) extra beyond the in-place sort
    def max_frequency(self, nums: list[int], k: int) -> int:
        nums.sort()
        left = 0
        total = 0
        best = 0
        for right, val in enumerate(nums):
            total += val
            while (right - left + 1) * val - total > k:
                total -= nums[left]
                left += 1
            best = max(best, right - left + 1)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) for the sort, O(n) for the sliding window | O(1) extra beyond the in-place sort |

## Tags

[NeetCode All](/catalog/neetcode).


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