Maximum Beauty of an Array After Applying Operation - Problem

You are given a 0-indexed array nums and a non-negative integer k. Your goal is to maximize the beauty of the array through strategic operations.

What can you do? For each index i in the array (you can use each index only once), you can replace nums[i] with any integer in the range [nums[i] - k, nums[i] + k].

What is beauty? The beauty of an array is the length of the longest subsequence consisting of equal elements. Remember, a subsequence maintains the original order but can skip elements.

Your mission: Return the maximum possible beauty after applying the operation optimally.

Example: If nums = [4,6,1,2] and k = 2, you could transform it to [4,4,3,4] by choosing appropriate values within each element's allowed range, giving a beauty of 3 (three 4's).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [4,6,1,2], k = 2
โ€บ Output: 3
๐Ÿ’ก Note: We can transform the array to [4,4,3,4] by choosing values within each element's range: nums[0]=4 stays 4, nums[1]=6 becomes 4 (within [4,8]), nums[2]=1 becomes 3 (within [-1,3]), nums[3]=2 becomes 4 (within [0,4]). The longest subsequence of equal elements is [4,4,4] with length 3.
example_2.py โ€” All Same Target
$ Input: nums = [1,1,1,1], k = 10
โ€บ Output: 4
๐Ÿ’ก Note: All elements are already the same, so we can keep them all as 1. The beauty is 4.
example_3.py โ€” No Overlap
$ Input: nums = [1,10,20], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: The ranges are [โˆ’1,3], [8,12], and [18,22]. No two ranges overlap, so we can't make any two elements equal. Maximum beauty is 1.

Visualization

Tap to expand
๐ŸŽผ Orchestra Harmony ProblemMusicians with instruments that can be tuned ยฑk semitones๐ŸŽบNote: C4Range: A#3-D4๐ŸŽทNote: D4Range: C4-E4๐ŸŽปNote: A3Range: G3-B3๐ŸŽธNote: B3Range: A3-C#4After Sorting by Natural Pitch๐ŸŽปA3๐ŸŽธB3๐ŸŽบC4๐ŸŽทD4These 3 can all play C4!๐ŸŽป: A3โ†’C4 (+3), ๐ŸŽธ: B3โ†’C4 (+1), ๐ŸŽบ: C4โ†’C4 (0)๐ŸŽฏ Maximum Harmony = 3 MusiciansJust like maximum beauty = 3 elements!
Understanding the Visualization
1
Line Up Musicians
Sort musicians by their natural pitch (sort the array)
2
Find Harmony Groups
Musicians close in pitch can more easily play the same note
3
Sliding Window
Use a window to find the largest group where range โ‰ค 2k
4
Maximum Harmony
The largest valid group gives us the maximum beauty
Key Takeaway
๐ŸŽฏ Key Insight: After sorting, elements that can be made equal form contiguous groups where the range is at most 2k. Use sliding window to find the largest such group efficiently!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting takes O(n log n), then O(n) binary searches each taking O(log n)

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using a few variables for indices and results

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i], k โ‰ค 105
  • Each index can only be used once
  • A subsequence maintains the original order
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen