C++ Simple *ONE* Map & deque
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUse one map which store
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse one map which stores the key and count. It auoto sorts the data.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\ncalculateMKAverage: O(k)\naddElement: O(m)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m)\n# Code\n```\n#include <ranges>\nclass MKAverage {\npublic:\n    deque<int> q;\n    map<int,int> m;\n    int M,K;\n    long total=0;\n    MKAverage(int m, int k) {\n        this->M=m;\n        this->K=k;\n    }\n    \n    void addElement(int num) {\n        q.push_back(num);\n        m[num]++;\n        total+=num;\n        if(q.size()>M){\n            m[q.front()]--;\n            if(m[q.front()]==0)\n                m.erase(q.front());\n            total-=q.front();\n            q.pop_front();\n\n        }\n    }\n    \n    int calculateMKAverage() {\n        if(q.size()<M)\n            return -1;\n        int j=K;\n        int toRemove=0,toRemoveR=0;;\n        for(auto  & [k, v]:m){\n            if(j>v){\n                toRemove+=k*v;\n                j-=v;\n                continue;\n            }else{\n                toRemove+=k*j;\n                break;\n            }\n\n        }\n        j=K;\n        for (auto  & [k, v] : m | std::views::reverse){\n           if(j>v){\n                toRemoveR+=k*v;\n                j-=v;\n                continue;\n            }else{\n                toRemoveR+=k*j;\n                break;\n            }\n        } \n        return (total - toRemove - toRemoveR) / (M-2*K);\n    }\n};\n\n/**\n * Your MKAverage object will be instantiated and called as such:\n * MKAverage* obj = new MKAverage(m, k);\n * obj->addElement(num);\n * int param_2 = obj->calculateMKAverage();\n */\n```
['C++']