C++ simple solution using sliding window.
Intuition\nIf the number is present in the array nums, find max elements that can be transformed into the number.\nElse find max numbers that can be transformed
# Intuition\nIf the number is present in the array nums, find max elements that can be transformed into the number.\nElse find max numbers that can be transformed to be equal.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\n    int max2K(vector<int>& nums, int k) {\n        int res = 0;\n        for(int i=0, j=0; j<nums.size(); j++) {\n            while(nums[i] < nums[j] - (2*k)) ++i;\n            res = max(res, j-i+1);\n        }\n        return res;\n    }\npublic:\n    int maxFrequency(vector<int>& nums, int k, int n) {\n        sort(nums.begin(), nums.end());\n        unordered_map<int, int> freq;\n        vector<int> f(nums.size()), b(nums.size());\n        for(int i=0, j=0; j<nums.size(); j++) {\n            freq[nums[j]]++;\n            while(nums[i] < nums[j]-k) i++;\n            f[j] = j-i+1;\n        }\n\n        for(int i=nums.size()-1, j=nums.size()-1; i>=0; i--) {\n            while(nums[i]+k < nums[j]) j--;\n            b[i] = j-i+1;\n        }\n\n        int res = 0;\n        for(int i=0; i<nums.size(); i++) {\n            int val = min((f[i] + b[i] - 1 - freq[nums[i]]), n);\n            res = max(res, freq[nums[i]] + val);\n        }\n        return max(res, min(max2K(nums, k), n));\n    }\n};\n```
['C++']