Straight forward solution
Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque 
# Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque is suitable for the rolling window, and a Counter can efficiently track number frequencies.\n\n# Approach\n1. Initialization: Use a deque to maintain the last m numbers and a Counter for their frequencies. Track the sum of middle numbers with middle_sum.\n2. addElement: On new number addition, if deque is full, remove the oldest number. Update deque and Counter. If deque has m numbers, compute the middle_sum by deducting the smallest and largest k values.\n3. calculateMKAverage: If deque size is less than m, return -1. Otherwise, return middle_sum divided by (m - 2 * k).\n\n# Complexity\n- Time complexity:\naddElement is O(m log m) due to sorting. calculateMKAverage is O(1).\n\n- Space complexity:\nThe deque will hold at most m elements and the counter will hold, in the worst case, m unique elements. This gives a space complexity of O(m).\n\n# Code\n```\nclass MKAverage:\n\n    def __init__(self, m: int, k: int):\n        self.m, self.k = m, k\n        self.queue = deque()\n        self.counter = Counter()\n        self.middle_sum = 0\n        self.qlen = 0\n\n    def addElement(self, num: int) -> None:\n        # If m numbers already in queue, remove the leftmost number\n        if len(self.queue) == self.m:\n            old = self.queue.popleft()\n            self.counter[old] -= 1\n            if self.counter[old] == 0:\n                del self.counter[old]\n        else:\n            self.qlen += 1\n\n        self.queue.append(num)\n        self.counter[num] += 1\n\n        # If m numbers in the queue, recompute the middle_sum\n        if len(self.queue) == self.m:\n            self.middle_sum = sum(num * count for num, count in self.counter.items())\n            sorted_items = sorted(self.counter.items())\n            \n            # Deduct smallest k\n            c = 0\n            remain = self.k\n            while remain:\n                if sorted_items[c][1] >= remain:\n                    self.middle_sum -= (sorted_items[c][0] * remain)\n                    break\n                else:\n                    self.middle_sum -= (sorted_items[c][0] * sorted_items[c][1])\n                    remain -= sorted_items[c][1]\n                c += 1\n\n            # Deduct largest k\n            c = -1\n            remain = self.k\n            while remain:\n                if sorted_items[c][1] >= remain:\n                    self.middle_sum -= (sorted_items[c][0] * remain)\n                    break\n                else:\n                    self.middle_sum -= (sorted_items[c][0] * sorted_items[c][1])\n                    remain -= sorted_items[c][1]\n                c -= 1\n\n    def calculateMKAverage(self) -> int:\n        if self.qlen < self.m:\n            return -1\n        return self.middle_sum // (self.m - 2 * self.k)\n\n# Your MKAverage object will be instantiated and called as such:\n# obj = MKAverage(m, k)\n# obj.addElement(num)\n# param_2 = obj.calculateMKAverage()\n```
['Python3']