Watch 10 video solutions for Maximum Beauty of an Array After Applying Operation, a medium level problem involving Array, Binary Search, Sliding Window. This walkthrough by NeetCodeIO has 8,935 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed array nums and a non-negative integer k.
In one operation, you can do the following:
i that hasn't been chosen before from the range [0, nums.length - 1].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.
Example 1:
Input: nums = [4,6,1,2], k = 2 Output: 3 Explanation: In this example, we apply the following operations: - 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]. After the applied operations, 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.
Example 2:
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 <= 1050 <= nums[i], k <= 105Problem Overview: You are given an integer array nums and a value k. Each element can be increased or decreased by at most k. After performing the operation on every element, the goal is to maximize the beauty of the array, defined as the largest number of equal elements. The task reduces to finding the largest group of values that can be adjusted to the same number.
Approach 1: Sorting and Two Pointer Technique (O(n log n) time, O(1) space)
Sort the array first using sorting. If two numbers differ by at most 2 * k, they can be adjusted to the same value because each can move within the range [num - k, num + k]. After sorting, use a sliding window with two pointers. Expand the right pointer while the condition nums[right] - nums[left] <= 2 * k holds. If the difference exceeds this limit, move the left pointer forward to shrink the window. The window size represents how many elements can be transformed into the same value. Track the maximum window length during the scan. Sorting costs O(n log n) time and the two-pointer pass is linear.
Approach 2: HashMap Frequency Counting with Range Sweep (O(n log n) time, O(n) space)
Instead of sorting values directly, treat each number as an adjustable interval [num - k, num + k]. Use a HashMap or ordered map to perform a line sweep. Add +1 at num - k and -1 at num + k + 1. After inserting all updates, iterate through the sorted keys and maintain a running prefix sum to compute how many intervals overlap at each point. The highest overlap corresponds to the maximum beauty. This method resembles prefix-difference techniques often used with arrays and interval problems. Sorting the keys leads to O(n log n) time complexity with O(n) extra space.
Recommended for interviews: The sorting + two pointer solution is the most expected answer. It shows strong understanding of sliding window patterns and how value ranges translate into window constraints. The interval sweep idea is also valid, but interviewers typically prefer the two-pointer approach because it is simpler, memory efficient, and easier to implement under time pressure.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + Two Pointer Sliding Window | O(n log n) | O(1) | Best general solution; simple implementation and optimal for interviews |
| HashMap Interval Sweep (Prefix Sum) | O(n log n) | O(n) | Useful when modeling the problem as overlapping ranges or interval counting |