Fastest Solution | Most Optimized One | Explained
Intuition\n Describe your first thoughts on how to solve this problem. \nTo maximize the frequency of any single element in the given nums array after performin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo maximize the frequency of any single element in the given nums array after performing at most numOperations. These operations allow incrementing the elements of the array, and you can increase their values to match others, thereby increasing the frequency of elements.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTo find the maximum frequency of any number in an array after performing at most numOperations allowed increments, sort the array and use a sliding window with binary search to efficiently count how many elements can be transformed into the current number. Maintain a counter to track the frequency and return the highest frequency achieved during the process.\n\n\n# Complexity\n- Time complexity: O(n\\log n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n) \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:\n        ans = 1\n        numscounter = Counter(nums)\n        nums.sort()\n        for i in range(len(nums)):\n            ans = max(ans, numscounter[nums[i]])\n            ind = bisect_right(nums, nums[i]+2*k)\n            ans = max(ans, min(ind-i,numOperations))\n            ind = bisect_left(nums, nums[i]-2*k)\n            ans = max(ans, min(i-ind+1,numOperations))\n            ind1 = bisect_right(nums, nums[i]+k)\n            ind2 = bisect_left(nums, nums[i]-k)\n            ans = max(ans, min(ind1-i+i-ind2,numOperations+numscounter[nums[i]]))\n        return ans\n\n```\n\n```C++ []\nclass Solution {\npublic:\n    int maxFrequency(std::vector<int>& nums, int k, int numOperations) {\n        int ans = 1;\n        std::unordered_map<int, int> numscounter; // Counter for frequencies\n        std::sort(nums.begin(), nums.end()); // Sort the nums array\n\n        for (int i = 0; i < nums.size(); i++) {\n            numscounter[nums[i]]++; // Count occurrences of each number\n            ans = std::max(ans, numscounter[nums[i]]); // Update the maximum frequency\n            \n            // Find bounds using binary search\n            int ind = std::upper_bound(nums.begin(), nums.end(), nums[i] + 2 * k) - nums.begin();\n            ans = std::max(ans, std::min(ind - i, numOperations)); // Frequency by increasing\n            \n            ind = std::lower_bound(nums.begin(), nums.end(), nums[i] - 2 * k) - nums.begin();\n            ans = std::max(ans, std::min(i - ind + 1, numOperations)); // Frequency by decreasing\n            \n            int ind1 = std::upper_bound(nums.begin(), nums.end(), nums[i] + k) - nums.begin();\n            int ind2 = std::lower_bound(nums.begin(), nums.end(), nums[i] - k) - nums.begin();\n            ans = std::max(ans, std::min(ind1 - i + i - ind2, numOperations + numscounter[nums[i]]));\n        }\n\n        return ans;\n    }\n};\n\n```\n\n```Java []\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int numOperations) {\n        int ans = 1;\n        HashMap<Integer, Integer> numscounter = new HashMap<>(); // Counter for frequencies\n        Arrays.sort(nums); // Sort the nums array\n\n        for (int i = 0; i < nums.length; i++) {\n            numscounter.put(nums[i], numscounter.getOrDefault(nums[i], 0) + 1); // Count occurrences\n            ans = Math.max(ans, numscounter.get(nums[i])); // Update the maximum frequency\n            \n            // Find bounds using binary search\n            int ind = upperBound(nums, nums[i] + 2 * k);\n            ans = Math.max(ans, Math.min(ind - i, numOperations)); // Frequency by increasing\n            \n            ind = lowerBound(nums, nums[i] - 2 * k);\n            ans = Math.max(ans, Math.min(i - ind + 1, numOperations)); // Frequency by decreasing\n            \n            int ind1 = upperBound(nums, nums[i] + k);\n            int ind2 = lowerBound(nums, nums[i] - k);\n            ans = Math.max(ans, Math.min(ind1 - i + i - ind2, numOperations + numscounter.get(nums[i])));\n        }\n\n        return ans;\n    }\n\n    // Function to find the index of the upper bound\n    private int upperBound(int[] arr, int target) {\n        int left = 0, right = arr.length;\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n            if (arr[mid] <= target) {\n                left = mid + 1;\n            } else {\n                right = mid;\n            }\n        }\n        return left;\n    }\n\n    // Function to find the index of the lower bound\n    private int lowerBound(int[] arr, int target) {\n        int left = 0, right = arr.length;\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n            if (arr[mid] < target) {\n                left = mid + 1;\n            } else {\n                right = mid;\n            }\n        }\n        return left;\n    }\n}\n```\n\n
['Binary Search', 'C++', 'Java', 'Python3']