↓Skip to main content
  1. LeetCode/

LeetCode 2779: Maximum Beauty of an Array After Applying Operation

·2 mins· ·
LeetCode Medium Sliding-Window Sorting Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-11

  • Submissions: Time Limit Exceeded → Accepted

Approach
#

Each number x can become any value in [x - k, x + k]. A group can become one value ⇔ their ranges share a common point ⇔ max − min <= 2k.

The problem asks for a subsequence (not contiguous), so sort first; it becomes “the longest window with nums[right] - nums[left] <= 2k”.

My Attempts
#

Attempt 1 counted every integer in [x - k, x + k] for each number and took the maximum, O(n · k), which hit TLE with k up to 10^5.

(That idea can be made O(n + value range) with a difference array, but sorting plus a sliding window is more direct.)

Solution
#

class Solution:
    def maximumBeauty(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = best = 0
        for right, x in enumerate(nums):
            while x - nums[left] > 2 * k:
                left += 1
            best = max(best, right - left + 1)
        return best

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Counting every integer in each range: O(n · k).
  • The bound is 2k, not k: both ends can move k toward each other.

Interview Explanation
#

Each number can become anything within k of itself, so a group can be made equal when max minus min is at most 2k. The problem wants a subsequence, so I sort and use a sliding window for the longest stretch with max minus min <= 2k. Time O(n log n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 220: Contains Duplicate III
·3 mins
LeetCode Hard Sliding-Window Bucket-Sort Ordered-Set Sorting Array
LeetCode 220 note: with bucket size valueDiff + 1, two numbers in one bucket always qualify, so only the two neighboring buckets need checking; the window keeps the latest indexDiff numbers, so each bucket holds at most one