Simple and easy to understand
Intuition\n1. use List to maintain the order\n2. Use Treemap to maintain the sorted order \n3. In add method check the size and take action accordingly \n4. In 
# Intuition\n1. use List to maintain the order\n2. Use Treemap to maintain the sorted order \n3. In add method check the size and take action accordingly \n4. In calculate method skip first k elament and last k element \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n-     Time complexity:\n- addElement(int num)=  O(log n)\n- calculateMKAverage() =  O(m * log n)\n\n-     Space complexity:\n- O(m) \n\n# Code\n```\n\nclass MKAverage {\n    private int m, k;\n    private List<Integer> stream;\n    private TreeMap<Integer, Integer> minMap;\n    private long sum;\n\n    public MKAverage(int m, int k) {\n        this.m = m;\n        this.k = k;\n        this.stream = new ArrayList<>();\n        this.minMap = new TreeMap<>();      \n        this.sum = 0;\n    }\n\n    public void addElement(int num) {\n        stream.add(num);\n        sum +=num;\n        minMap.put(num, minMap.getOrDefault(num, 0)+1);\n\n        if(stream.size()>m){\n            int val = stream.get(0);\n            sum -=val;\n            \n            int count = minMap.get(val);\n            if(count==1)\n            minMap.remove(val);\n            else minMap.put(val,count-1);\n            \n            stream.remove(0);\n        }\n    }\n\n\n    public int calculateMKAverage() {\n        if (stream.size() < m) {\n            return -1;\n        }\n\n       long excludeSum=0;\n\n        int counter =0;\n        int last = m-k;\n        for(int key : minMap.keySet()){\n            int count = minMap.get(key);\n            while(count>0){\n            if(counter<k || counter>=last)\n            excludeSum += key;\n            count--;\n            counter++;\n            }\n           \n        }\n\n        return (int) ((sum - excludeSum) / (m - 2 * k));\n    }\n\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```
['Java']