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
Visualization
Time & Space Complexity
Sorting takes O(n log n), then O(n) binary searches each taking O(log n)
Only using a few variables for indices and results
Constraints
- 1 โค nums.length โค 105
- 0 โค nums[i], k โค 105
- Each index can only be used once
- A subsequence maintains the original order