Line sweep , 10 lines code
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxFrequency(vector<int>& arr, int k, int num) {\n        map<int,int> p; unordered_map<int,int> mp;\n        int ans = 0;\n        for(auto x : arr){\n            p[x-k]++;\n            p[x+k+1]--;\n            p[x]+=0;\n            mp[x]++;\n        }\n        int line = 0;\n        for(auto p : p){\n            int x = p.first; int y = p.second;\n            line+=y;\n            ans = max(ans, min(line-mp[x],num)+mp[x]);\n        }\n        return ans;\n    }\n\n};\n```
['C++']