C++ 10 lines
\nint maxFrequency(vector<int>& nums, int k, int numOperations) {\n\tsort(begin(nums), end(nums));        \n\tauto search = [&](auto i) {\n\t\tint repeatedElems
```\nint maxFrequency(vector<int>& nums, int k, int numOperations) {\n\tsort(begin(nums), end(nums));        \n\tauto search = [&](auto i) {\n\t\tint repeatedElems = upper_bound(begin(nums), end(nums), i) - lower_bound(begin(nums), end(nums), i);\n\t\tint numElems = upper_bound(begin(nums), end(nums), i+k) - lower_bound(begin(nums), end(nums), i-k);\n\t\treturn min(numElems-repeatedElems, numOperations) + repeatedElems;\n\t};        \n\treturn accumulate(begin(nums), end(nums), 0, [&](auto r, auto i){\n\t\treturn max({r, search(i), search(i+k)});\n\t});\n}\n```\n\nIntutiton: Suppose there exists a number `i` with maximum frequency:\n- `i` exists in the `nums`: Pick a number `x`  (**excluding i**) in the vicinity of `i` so that upon subtracting a number between `[-k,k]` from `x`, the result becomes equal to `i`. Now, pick such `x` atmost `numOperations` times.\n- `i` does not exist in the `nums`: Pick a number `x`  (**including i**) in the vicinity of `i` so that upon subtracting a number between `[-k,k]` from `x`, the result becomes equal to `i`. Now, pick such `x` atmost `numOperations` times.
[]