You are given two integers, m and k, and a stream of integers. You need to implement a data structure that calculates the MKAverage for the stream.

The MKAverage can be calculated using these steps:

  1. If the number of elements in the stream is less than m, the MKAverage is -1.
  2. Otherwise, copy the last m elements of the stream to a separate container.
  3. Remove the smallest k elements and the largest k elements from the container.
  4. Calculate the average value for the remaining elements, rounded down to the nearest integer.

Implement the MKAverage class:

  • MKAverage(int m, int k) Initializes the MKAverage object with an empty stream and the two integers m and k.
  • void addElement(int num) Inserts a new element num into the stream.
  • int calculateMKAverage() Calculates and returns the MKAverage for the current stream rounded down to the nearest integer.

Input & Output

Example 1 — Basic Operations
$ Input: MKAverage(3, 1), addElement(3), addElement(1), addElement(10), addElement(5), addElement(5), addElement(5), calculateMKAverage()
Output: [null, null, null, null, null, null, null, 5]
💡 Note: After adding [3,1,10,5,5,5], last 3 elements are [5,5,5]. Remove 1 smallest and 1 largest: middle element is 5, so average is 5.
Example 2 — Insufficient Elements
$ Input: MKAverage(3, 1), addElement(3), addElement(1), calculateMKAverage()
Output: [null, null, null, -1]
💡 Note: Only 2 elements in stream, but need m=3 elements minimum, so return -1.
Example 3 — Different Values
$ Input: MKAverage(5, 2), addElement(3), addElement(1), addElement(12), addElement(5), addElement(3), addElement(4), calculateMKAverage()
Output: [null, null, null, null, null, null, null, 4]
💡 Note: Last 5 elements: [1,12,5,3,4]. Sorted: [1,3,4,5,12]. Remove 2 smallest [1,3] and 2 largest [5,12]. Middle: [4]. Average = 4.

Constraints

  • 1 ≤ m ≤ 105
  • 1 ≤ k*2 < m
  • 1 ≤ num ≤ 105
  • At most 105 calls to addElement and calculateMKAverage

Visualization

Tap to expand
MK Average Calculator INPUT Stream of integers: 3 1 10 5 5 5 Parameters: m = 3 (window size) k = 1 (elements to trim) Last m=3 elements: 5 5 5 ALGORITHM STEPS 1 Check Count count >= m? 6 >= 3 = YES 2 Get Last m Elements Copy [5, 5, 5] to container 3 Remove k Smallest/Largest Remove 1 smallest, 1 largest 5 5 5 Sorted: [5, 5, 5] After trim: [5] 4 Calculate Average sum / count = 5 / 1 = 5 MKAverage = sum(middle) / len = 5 / 1 = 5 (rounded down) FINAL RESULT MKAverage Result: 5 OK Complete Output: [null, null, null, null, null, null, 5] 6 addElement ops + 1 calculate Balanced BST Approach O(log m) per operation Using 3 multisets for sections Key Insight: Use three balanced BSTs (multisets) to maintain: smallest k elements, middle (m-2k) elements, and largest k elements. When adding/removing elements, rebalance between sets. Track running sum of middle set for O(1) average calculation. A sliding window queue tracks the last m elements, enabling efficient removal of oldest element. TutorialsPoint - Finding MK Average | Optimized with Balanced Data Structures
Asked in
Amazon 15 Google 12 Microsoft 8 Apple 6
28.5K Views
Medium Frequency
~45 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen