Input: nums =[1,4,5], k =1, numOperations =2Output: 2Explanation:
We can achieve a maximum frequency of two by:* Adding 0 to `nums[1]`.`nums` becomes `[1, 4, 5]`.* Adding -1 to `nums[2]`.`nums` becomes `[1, 4, 4]`.
Each input value v defines an inclusive interval of targets it can be moved to: [v - k, v + k]. If a target t lies inside that interval then the element v can be changed (with one operation) to become t. So for any candidate target t the number of elements that can be turned into t equals the number of intervals that cover t. Elements already equal to t count for free. With at most numOperations changes we can convert up to numOperations additional covered elements. This is a classic sweep-line / difference-array application: add +1 at interval start and -1 after interval end, sweep the keys in order and track how many intervals cover each candidate point t.
Note: Method 1 does not sort the input array nums. The O(n log n) cost comes from ordering interval endpoints or using ordered-map (TreeMap / std::map / BTreeMap) operations, not from sorting nums itself.
Build a frequency map count of original values in nums (how many are already equal to a target).
Build a difference map diff (ordered map): for every v in nums do diff[v-k] += 1 and diff[v+k+1] -= 1. Also insert v into the set of keys so we evaluate the exact points that appear in nums.
Sweep the ordered keys of diff accumulating a running cover value. At each key t the cover value equals how many numbers have t in their [v-k, v+k] interval.
The achievable frequency at t is count.get(t, 0) + min(cover - count.get(t, 0), numOperations). Update the global maximum.
Return the global maximum.
This method examines only O(m) distinct keys where m is the number of unique endpoints / values, so overall complexity is O(n log n) dominated by ordered-map operations.
This approach is in a way very close to sweep line approach on the difference array.
classSolution {
public:int maxFrequency(std::vector<int>& nums, int k, int numOperations) {
if (nums.empty()) return0;
std::map<longlong, int> diff; // ordered keys
std::unordered_map<int,int> count;
for (int v : nums) {
count[v]++;
longlong a = (longlong)v - k;
longlong b = (longlong)v + k +1; // end + 1 for inclusive interval
diff[a] +=1;
diff[b] -=1;
// ensure exact value is present as a key so we evaluate it
diff[v] +=0;
}
int ans =0;
longlong cover =0;
for (auto&p : diff) {
cover += p.second;
int t = (int)p.first;
int base = count.count(t) ? count[t] :0;
longlong others = cover - base;
if (others <0) others =0;
int achievable = base + (int)std::min<longlong>(others, numOperations);
ans = std::max(ans, achievable);
}
return ans;
}
};
classSolution {
publicintmaxFrequency(int[] nums, int k, int numOperations) {
if (nums.length== 0) return 0;
Map<Integer, Integer> count =new HashMap<>();
TreeMap<Integer, Integer> diff =new TreeMap<>();
for (int v : nums) {
count.put(v, count.getOrDefault(v, 0) + 1);
int a = v - k;
int b = v + k + 1;
diff.put(a, diff.getOrDefault(a, 0) + 1);
diff.put(b, diff.getOrDefault(b, 0) - 1);
diff.putIfAbsent(v, 0); // ensure we evaluate exact values }
int ans = 0;
int cover = 0;
for (Map.Entry<Integer, Integer> e : diff.entrySet()) {
int point = e.getKey();
cover += e.getValue();
int base = count.getOrDefault(point, 0);
int others = cover - base;
if (others < 0) others = 0;
int achievable = base + Math.min(others, numOperations);
ans = Math.max(ans, achievable);
}
return ans;
}
}
classSolution {
funmaxFrequency(nums: IntArray, k: Int, numOperations: Int): Int {
if (nums.isEmpty()) return0val count = mutableMapOf<Int, Int>()
val diff = java.util.TreeMap<Int, Int>()
for (v in nums) {
count[v] = count.getOrDefault(v, 0) + 1val a = v - k
val b = v + k + 1 diff[a] = diff.getOrDefault(a, 0) + 1 diff[b] = diff.getOrDefault(b, 0) - 1 diff.putIfAbsent(v, 0)
}
var ans = 0var cover = 0for ((point, delta) in diff) {
cover += delta
val base = count.getOrDefault(point, 0)
var others = cover - base
if (others < 0) others = 0val achievable = base + kotlin.math.min(others, numOperations)
ans = kotlin.math.max(ans, achievable)
}
return ans
}
}
use std::collections::HashMap;
use std::collections::BTreeMap;
impl Solution {
pubfnmaxFrequency(nums: Vec<i32>, k: i32, numOperations: i32) -> i32 {
if nums.is_empty() { return0 }
letmut count: HashMap<i32, i32>= HashMap::new();
letmut diff: BTreeMap<i64, i32>= BTreeMap::new();
for&v in&nums {
*count.entry(v).or_insert(0) +=1;
let a = v asi64- k asi64;
let b = v asi64+ k asi64+1;
*diff.entry(a).or_insert(0) +=1;
*diff.entry(b).or_insert(0) -=1;
diff.entry(v asi64).or_insert(0);
}
letmut ans =0;
letmut cover: i32=0;
for (point, delta) in diff {
cover += delta;
let t = point asi32;
let base =*count.get(&t).unwrap_or(&0);
letmut others = cover - base;
if others <0 { others =0 }
let achievable = base + std::cmp::min(others, numOperations);
ans = std::cmp::max(ans, achievable);
}
ans
}
}