Easy understanding solution by a sorted map + sliding window with different situation. C++
Intuition\nWe can divide the answer into 2 situations.\n1. the middle value is a value in nums.\n- EX: [1,3,5], k = 2, numOperations = 2, move 1->3, 5->3\n2. th
# Intuition\nWe can divide the answer into 2 situations.\n1. the middle value is a value in nums.\n- EX: [1,3,5], k = 2, numOperations = 2, move 1->3, 5->3\n2. the middle value is not a value in nums.\n- EX: [1,5], k = 2, numOperations = 2, move 1->3, 5->3\n\nDeal with it separately.\n\n# Approach\nWe first count the appear time of each value into a map **m**.\n1. For the first situation, we can easy move the sliding window from each val in a map. \nEach time move the iterator to count the total number of values that are in range [iterMiddle-k, iterMiddle+k].\n- Move iterLeft until iterLeft\'s value >= iterMid\'s value - k \n- Move iterRight until iterRight\'s value > iterMid\'s value + k\nExample: [1,3,5], k = 2, numOperations = 2\nRange will be [-1,3] have 2 numbers \n=> [1,5] have 3 numbers \n=> [3,7] have 2 numbers\n\n2. For the second situation, we can think in **another way**. \nAlthough the middle value in not appear in nums, the leftest value of a range will always appear in nums to get the **most total number** of count. \nTherefore, we can count the total number in range [iterLeft, iterLeft + 2k] by moving iterRight.\n- Move iterRight until iterRight\'s value > iterLeft\'s value + 2*k\nExample: [1,5], k = 2, numOperations = 2\nRange will be [1,5] have 2 numbers \n=> [5,9] have 1 numbers \n\nFinally, we can summary two situations to get the final result.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxFrequency(vector<int>& nums, int k, int numOperations) {\n        map<long long, int> m; // val, count\n        for(auto i:nums){\n            m[i] += 1;\n        }\n        int res = 0;\n        int total = 0; // count the total number in the range\n        auto iterL = m.begin();\n        auto iterR = m.begin();\n        // second situation [iterL, iterL+2k]\n        while(iterL != m.end()){\n            while(iterR != m.end() && iterR->first <= iterL->first + 2*k){\n                total += iterR->second;\n                ++iterR;\n            }\n            res = max(res, total > numOperations ? numOperations : total);\n            if(iterR == m.end()) // early break\n                break;\n            total -= iterL->second;\n            ++iterL;\n        }\n        // first situation [iterM - k, iterM + k]\n        total = 0;\n        iterL = m.begin();\n        auto iterM = m.begin();\n        iterR = m.begin();\n        while(iterM != m.end()){\n            // find the total number in range [iterM - k, iterM + k]\n            while(iterR != m.end() && iterR->first <= iterM->first + k){\n                total += iterR->second;\n                ++iterR;\n            }\n            while(iterL->first < iterM->first - k){\n                total -= iterL->second;\n                ++iterL;\n            }\n            res = max(res, iterM->second + ((total - iterM->second) > numOperations ? numOperations : (total - iterM->second)));\n            ++iterM;\n        }\n        \n        return res;\n    }\n};\n```
['C++']