Best Solution || Beats 100% of users || Easy to Understand
\n\n# Code\ncpp []\n#define all(n) n.begin(),n.end()\nclass Solution {\npublic:\n    int maxFrequency(vector<int>& nums, int k, int numOp) \n    {\n        int 
![image.png](https://assets.leetcode.com/users/images/972b3cab-4d88-43ac-bfbc-1b1b6b4bd1a1_1731334815.5849478.png)\n\n# Code\n```cpp []\n#define all(n) n.begin(),n.end()\nclass Solution {\npublic:\n    int maxFrequency(vector<int>& nums, int k, int numOp) \n    {\n        int n= nums.size();\n        unordered_map<int,int> freq;\n        // calculating freq of all elements\n        for(auto x : nums) ++freq[x];\n\n       int ans =0,j=0;\n       sort(all(nums)); // sorting the array\n       for(int i=0 ; i<n ; i++) \n       {\n         // nums[i]-k to nums[i]+k sab nums[i] me convert ho jayege\n         int numbersCanConvert = upper_bound(all(nums),nums[i]+k) - lower_bound(all(nums),nums[i]-k);\n         // agr numOp me hai toh convert all numbersCanConvert else (numOpeartions + freq[nums[i]] ) jitne ho sakte hai\n         ans = max(ans,min(numbersCanConvert,numOp+freq[nums[i]]));\n         // for the inbetween elements of nums[i] to nums[j+1](or nums[i+1])\n         while(j+1<n && nums[j+1]-nums[i]<=2*k)j++;\n         // freq of inbetween elements is 0 \n         ans = max(ans,min(j-i+1,numOp));\n       }\n       return ans;  \n    }\n};\n```
['C++']