[ C++ | Java | Kotlin ] Simple binary search solution, beats 100% 🚀
Complexity\n- Time complexity: O(n * log(n))\n\n- Space complexity: O(n)\n\n# Code\ncpp []\nclass Solution {\nprivate:\n    static int solve(const vector<long l
# Complexity\n- Time complexity: $$O(n * log(n))$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\nprivate:\n    static int solve(const vector<long long>& nums, int width, int numOperations, long long target) {\n        int j = upperBound(nums, target - 1);\n        int k = lowerBound(nums, target + 1);\n        int j2 = upperBound(nums, target - 2 * width - 1);\n        int k2 = lowerBound(nums, target + 2 * width + 1);\n        return k - j + 1 + min(j - j2 + k2 - k, numOperations);\n    }\n    \n    static int upperBound(const vector<long long>& nums, long long target) {\n        int left = 0, right = nums.size();\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n            if (nums[mid] >= target) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n        return right;\n    }\n    \n    static int lowerBound(const vector<long long>& nums, long long target) {\n        int pos = upperBound(nums, target);\n        return pos - (pos < nums.size() && nums[pos] == target ? 0 : 1);\n    }\n    \npublic:\n    int maxFrequency(vector<int>& input, int width, int numOperations) {\n        // Sort and convert to long long\n        sort(input.begin(), input.end());\n        vector<long long> nums;\n        for (int x : input) {\n            nums.push_back(2LL * x);\n        }\n        \n        int ans = 0;\n        for (long long num : nums) {\n            int c1 = solve(nums, width, numOperations, num);\n            int c2 = solve(nums, width, numOperations, num - 2 * width);\n            int c3 = solve(nums, width, numOperations, num + 2 * width);\n            ans = max(ans, max(c1, max(c2, c3)));\n        }\n        return ans;\n    }\n};\n```\n```java []\nclass Solution {\n    private static int solve(long[] nums, int width, int numOperations, long target) {\n        int j = upperBound(nums, target - 1);\n        int k = lowerBound(nums, target + 1);\n        int j2 = upperBound(nums, target - 2 * width - 1);\n        int k2 = lowerBound(nums, target + 2 * width + 1);\n        return k - j + 1 + Math.min(j - j2 + k2 - k, numOperations);\n    }\n\n    public int maxFrequency(int[] input, int width, int numOperations) {\n        long[] nums = Arrays.stream(input).sorted().mapToLong(i -> i * 2L).toArray();\n        int ans = 0;\n        for (long num : nums) {\n            int c1 = solve(nums, width, numOperations, num);\n            int c2 = solve(nums, width, numOperations, num - 2 * width);\n            int c3 = solve(nums, width, numOperations, num + 2 * width);\n            ans = Math.max(ans, Math.max(c1, Math.max(c2, c3)));\n        }\n        return ans;\n    }\n\n    private static int upperBound(long[] nums, long target) {\n        int left = 0, right = nums.length;\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n            if (nums[mid] >= target) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n        return right;\n    }\n\n    private static int lowerBound(long[] nums, long target) {\n        int pos = upperBound(nums, target);\n        return pos - (pos < nums.length && nums[pos] == target ? 0 : 1);\n    }\n}\n```\n```kotlin []\nclass Solution {\n    fun maxFrequency(input: IntArray, width: Int, numOperations: Int): Int {\n        val nums = input.sorted().map { it.toLong() * 2 }\n\n        fun solve(target: Long): Int {\n            val j = nums.upperBound(target - 1)\n            val k = nums.lowerBound(target + 1)\n            val j2 = nums.upperBound(target - 2 * width - 1)\n            val k2 = nums.lowerBound(target + 2 * width + 1)\n            return k - j + 1 + min(j - j2 + k2 - k, numOperations)\n        }\n\n        return nums.flatMap { listOf(solve(it), solve(it - 2 * width), solve(it + 2 * width)) }.max()\n    }\n\n    companion object {\n        private fun List<Long>.upperBound(target: Long, left: Int = 0, right: Int = size): Int {\n            if (left >= right) return right\n            val mid = left + (right - left) / 2\n            return if (this[mid] >= target) upperBound(target, left, mid) else upperBound(target, mid + 1, right)\n        }\n\n        private fun List<Long>.lowerBound(target: Long, left: Int = 0, right: Int = size) =\n            upperBound(target, left, right).let { it - if (it < size && this[it] == target) 0 else 1 }\n    }\n}\n```\n![image.png](https://assets.leetcode.com/users/images/7c0dd035-745e-453d-9886-9387c09eb22a_1731168980.1445205.png)\n
['Binary Search', 'C++', 'Java', 'Kotlin']