Java one treemap and deque solution
Maintain a deque so we can remove the m+1 element. Maintain current sum and tot elements.\n- Keep a treemap and remove m+1 element on each add.\n- To compute av
- Maintain a deque so we can remove the `m+1` element. Maintain current sum and tot elements.\n- Keep a treemap and remove `m+1` element on each `add`.\n- To compute avg get low `k` elements, and top `k` elements, and subtract from current sum.\n\n```\nclass MKAverage {\n\n    int tot = 0;\n    int sum = 0;\n    int m = 0;\n    int k = 0;\n    \n    TreeMap<Integer, Integer> map = new TreeMap<>();\n    Deque<Integer> deque = new ArrayDeque<>();\n    \n    public MKAverage(int m, int k) {\n        this.m = m;\n        this.k = k;\n    }\n    \n    public void addElement(int num) {\n        deque.offerLast(num);\n        map.put(num, map.getOrDefault(num, 0)+1);\n        sum += num;\n        tot++;\n        if (tot > m) {\n            int v = deque.pollFirst();\n            sum -= v;\n            tot--;\n            int tot = map.get(v);\n            if (tot == 1) map.remove(v);\n            else map.put(v, tot-1);\n        }\n        \n    }\n    \n    public int calculateMKAverage() {\n       if (tot < m) return -1;\n        int totLess = k;\n        int s = sum;\n        for (Map.Entry<Integer, Integer> e : map.entrySet()) {\n            if (totLess == 0) break;\n            int v = Math.min(totLess, e.getValue());\n            s -= e.getKey()*v;\n            totLess -= v;\n        }\n        \n        totLess = k;\n        for (Map.Entry<Integer, Integer> e : map.descendingMap().entrySet()) {\n            if (totLess == 0) break;\n            int v = Math.min(totLess, e.getValue());\n            s -= e.getKey()*v;\n            totLess -= v;\n        }\n        \n        return s/(tot-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```
[]