Maximum Beauty of an Array After Applying Operation

medium sorting sliding window binary search

Problem

You are given an array nums and a non-negative integer k. In one operation you may pick an index not chosen before and replace nums[i] with any value in [nums[i] − k, nums[i] + k] (each index at most once). The beauty is the length of the longest subsequence of equal elements. Return the maximum possible beauty.

Two elements a and b can be made equal iff their reachable ranges overlap, i.e. |a − b| ≤ 2k. After sorting, the task reduces to finding the longest window nums[i … j] with nums[j] − nums[i] ≤ 2k.

Inputnums = [4,6,1,2], k = 2
Output3
Sorted: [1,2,4,6]. Values 2, 4, 6 all reach 4 (spread 6−2 = 4 = 2k), so three indices share value 4.

def maximumBeauty(nums, k):
    nums.sort()                            # group reachable values together
    left = 0                               # window start over sorted nums
    best = 0                               # longest valid window seen
    for right in range(len(nums)):         # extend the window rightward
        while nums[right] - nums[left] > 2 * k:
            left += 1                       # spread too wide: drop left value
        best = max(best, right - left + 1)  # window length = matching count
    return best
function maximumBeauty(nums, k) {
  nums.sort((a, b) => a - b);              // group reachable values together
  let left = 0;                            // window start over sorted nums
  let best = 0;                            // longest valid window seen
  for (let right = 0; right < nums.length; right++) {
    while (nums[right] - nums[left] > 2 * k) {
      left++;                              // spread too wide: drop left value
    }
    best = Math.max(best, right - left + 1); // window length = matching count
  }
  return best;
}
int maximumBeauty(int[] nums, int k) {
    Arrays.sort(nums);                      // group reachable values together
    int left = 0;                           // window start over sorted nums
    int best = 0;                           // longest valid window seen
    for (int right = 0; right < nums.length; right++) {
        while (nums[right] - nums[left] > 2 * k) {
            left++;                         // spread too wide: drop left value
        }
        best = Math.max(best, right - left + 1); // window length
    }
    return best;
}
int maximumBeauty(vector<int>& nums, int k) {
    sort(nums.begin(), nums.end());         // group reachable values together
    int left = 0;                           // window start over sorted nums
    int best = 0;                           // longest valid window seen
    for (int right = 0; right < (int)nums.size(); right++) {
        while (nums[right] - nums[left] > 2 * k) {
            left++;                         // spread too wide: drop left value
        }
        best = max(best, right - left + 1); // window length
    }
    return best;
}
Time: O(n log n) Space: O(1) extra (in-place sort)