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 <= 105In this approach, we first sort the array. The idea is to use a two-pointer technique to choose a number range such that the numbers in this range can be adjusted by k to be the same. We sort the array and then for each element, expand the range using a two-pointer to see how many elements can be adjusted to fall in the range [nums[i]-k, nums[i]+k].
The function first sorts the array using quicksort. It then initializes two pointers, 'i' and 'j', both starting from the beginning of the array. 'i' represents the start of the window, and 'j' is the current element being considered for inclusion in the window. The while loop contracts the window by increasing 'i' if the difference between nums[j] and nums[i] is more than 2*k. The main goal is to find the maximum size of the window [i, j] where nums[j] can be adjusted to be equal to nums[i] with the allowed operations, represented by the length of this valid subsequence.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting, followed by O(n) for the two-pointer scan, resulting in O(n log n) overall.
Space Complexity: O(1), since no additional space apart from input is used.
In this approach, we use a HashMap to count the occurrences of each element.. Then, for every element in the array, determine how many other elements can be adjusted to equal it given the range constraints. This involves counting up possible element choices within the range [x-k, x+k] for each x after adjusting with known positions.
We define a fixed size frequency array that takes possible adjustments into account. The frequency of alterations for any number range is recorded, storing increases with maximum numbers found (adjusted by k). Utilizing this frequency setup culminates in determining greatest repetitions possible.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n + m) where n is the number of elements and m pertains to range demand (constant 200001).
Space Complexity: O(m) for frequency storage.
| Approach | Complexity |
|---|---|
| Approach 1: Sorting and Two Pointer Technique | Time Complexity: O(n log n) due to sorting, followed by O(n) for the two-pointer scan, resulting in O(n log n) overall. |
| Approach 2: Using a HashMap for Frequency Counting | Time Complexity: O(n + m) where n is the number of elements and m pertains to range demand (constant 200001). |
Maximum Beauty of an Array After Applying Operation | 3 Approaches | Leetcode 2779 |codestorywithMIK • codestorywithMIK • 8,022 views views
Watch 9 more video solutions →Practice Maximum Beauty of an Array After Applying Operation with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor