Explanation for beginners | Easy
 # Intuition  \n\n\n# Approach\n\n0. Sort the Array: Sorting the array helps to use binary search effectively and to focus on making elements in a contiguous ra
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n0. Sort the Array: Sorting the array helps to use binary search effectively and to focus on making elements in a contiguous range the same.\n\n1. Frequency Calculation with Target Adjustment:\nWe use a frequency map (um) to store how often each element appears in nums.\nFor each element in the sorted array, we calculate the maximum achievable frequency by making other nearby elements match this element within a range of [-k, k] adjustments.\n\n2. Binary Search for Range of Modifiable Elements:\n\nFor each element nums[i], we use lower_bound to find the start of the range of elements that can potentially be adjusted to match nums[i] - k or nums[i] + k.\nWe calculate how many elements can be adjusted to achieve the highest frequency for each target value within the allowed operations.\n\n# Complexity\n- Time complexity:$$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\n#define ll long long\nclass Solution {\npublic:\n    int maxFrequency(vector<int>& nums, int k, int ops) {\n        // Step 1: Sort nums to use binary search effectively\n        sort(begin(nums), end(nums));\n        \n        // Step 2: Create a frequency map to count occurrences of each number\n        unordered_map<ll, ll> um;\n        for (int i : nums) um[i]++;\n        \n        ll ans = 0; // Variable to store the maximum frequency\n        \n        // Step 3: First pass - Check potential targets around nums[i] - k\n        for (int i = 0; i < size(nums); i++) {\n            // Find the start of the range within which elements can be adjusted to nums[i] - k\n            int a = lower_bound(begin(nums), end(nums), nums[i] - 2 * k) - begin(nums);\n            int p = nums[i] - k; // Target value\n\n            // Update maximum frequency by adjusting within the range found\n            ans = max(ans, um[p] + min(i - a - um[p] + 1, (ll)ops));\n        }\n        \n        // Step 4: Second pass - Check potential targets around nums[i] + k\n        for (int i : nums) {\n            int a = lower_bound(begin(nums), end(nums), i - k) - begin(nums);\n            int b = upper_bound(begin(nums), end(nums), i + k) - begin(nums) - 1;\n\n            // Update maximum frequency by adjusting within the range found\n            ans = max(ans, um[i] + min(b - a - um[i] + 1, (ll)ops));\n        }\n        \n        return ans; // Return the maximum frequency achieved\n    }\n};\n\n```
['C++']