Problem Description
Given an integer array nums and two integers k and numOperations, you must perform exactly numOperations “operations” on distinct indices of nums. In each operation you can add any integer from -k to k to the chosen element. The goal is to maximize the frequency (the count of occurrences) of some element (target value) in nums after these operations. An element which is already equal to the target does not need to be changed, but if an element is not equal to the target it can be changed in one operation provided that the difference |nums[i] – target| is at most k. Return the maximum possible frequency of any element after performing the operations.
Key Insights
- An index can be modified at most once by adding a value from the range [-k, k].
- For any chosen target value T, an element x can become T if |x – T| ≤ k.
- If an element is already equal to T, no operation is needed.
- For a candidate target T: • Let freeCount = frequency of T already in nums. • Let eligibleCount = the total number of elements x with x in [T – k, T + k] (since these can be “converted” to T in one operation).
- The maximum frequency obtainable for target T is freeCount + min(numOperations, eligibleCount – freeCount).
- Also note that sometimes a target value not present in the array may yield a better result; in that case, all elements in the chosen “window” would require an operation and the maximum frequency is limited by numOperations (and by the size of that eligible window).
- Using sorting and binary search (or a sliding window) helps count how many elements lie in any interval [T – k, T + k].
- The eligible set for any candidate T is all array elements in an interval of fixed length (2*k + 1). This fact can be used in a sliding window over the sorted array.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting and binary search for each candidate unique number. Space Complexity: O(n) for storing the sorted array and frequency map.
Solution
We first sort the array and build a frequency map of the numbers. For each candidate target T that appears in the array, we use binary search on the sorted array to count the number of elements in the range [T – k, T + k]. The maximum frequency we can achieve with target T is the sum of freeCount (the number of elements already equal to T) and the minimum of numOperations and the number of eligible elements that are not T (i.e. eligibleCount – freeCount), so it is given by min(freeCount + numOperations, eligibleCount).
We also consider targets T that are not in the array. In that case the frequency equals the number of elements we can “convert” to T, but each conversion costs one operation and only elements within T ± k qualify. This reduces to a sliding window problem over the sorted array in which we find the maximum count of numbers whose difference is at most 2*k. The best possible frequency in that scenario will be min(maxWindowCount, numOperations).
The final answer is the maximum among these candidate frequencies.