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

# Maximum Beauty of an Array After Applying

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

LeetCode 2779, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Sliding Window](/catalog/topics/sliding-window), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/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 2779   # by problem number
lcpy gen -s maximum_beauty_of_an_array_after_applying_operation   # by problem name
```

## Problem

You are given a **0-indexed** array `nums` and a **non-negative** integer `k`.

In one operation, you can do the following:

* Choose an index `i` that **hasn't been chosen before** from the range `[0, nums.length - 1]`.
* Replace `nums[i]` with any integer from the range `[nums[i] - k, nums[i] + k]`.

The **beauty** of the array is the length of the longest subsequence consisting of equal elements.

Return *the **maximum** possible beauty of the array* `nums` *after applying the operation any number of times.*

**Note** that you can apply the operation to each index **only once**.

A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.

### Examples

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

**Explanation:** Choose index 1, replace it with 4 (from range \[4,8]), nums = \[4,4,1,2]. Choose index 3, replace it with 4 (from range \[0,4]), nums = \[4,4,1,4]. The beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve.

```
Input: nums = [1,1,1,1], k = 10
Output: 4
```

**Explanation:** In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array).

### Constraints

* `1 <= nums.length <= 10^5`
* `0 <= nums[i], k <= 10^5`

## Solution

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

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

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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