Javascript - Sort + Map
Code\njavascript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} numOperations\n * @return {number}\n */\nvar maxFrequency = functi
# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} numOperations\n * @return {number}\n */\nvar maxFrequency = function (nums, k, numOperations) {\n  const n = nums.length;\n  nums.sort((a, b) => a - b);\n  const map = new Map();\n  let ans = 0;\n\n  // case 1: existing element as potential target\n  let i = 0;\n  let j = 0;\n  for (const num of nums) {\n    // increase gap\n    while (j < n && nums[j] <= num + k) {\n      map.set(nums[j], (map.get(nums[j]) || 0) + 1);\n      j++;\n    }\n\n    // decrease gap\n    while (i < n && nums[i] < num - k) {\n      map.set(nums[i], (map.get(nums[i]) || 0) - 1);\n      i++;\n    }\n\n    const curr = Math.min(j - i, map.get(num) + numOperations);\n    ans = Math.max(ans, curr);\n  }\n\n  // case 2: non existing element as potential target\n  j = 0;\n  for (let i = 0; i < n; i++) {\n    while (j < n && nums[j] <= nums[i] + 2 * k) {\n      j++;\n    }\n    const curr = Math.min(j - i, numOperations);\n    ans = Math.max(ans, curr);\n  }\n\n  return ans;\n};\n```
['Hash Table', 'Sorting', 'JavaScript']