Finding MK Average
Try to solve the Finding MK Average problem.
We'll cover the following...
Statement
You are given two integers, m
and k
, and a stream of integers. Your task is to design and implement a data structure that efficiently calculates the MK Average for the stream.
To compute the MK Average, follow these steps:
Stream length check: If the stream contains fewer than
m
elements, return-1
as the MK Average.Window selection: Otherwise, copy the last
m
elements of the stream to a separate container and remove the smallestk
elements and the largestk
elements from the container.Average calculation: Calculate the average of the remaining elements (rounded down to the nearest integer).
Implement the MKAverage
class
MKAverage(int m, int k)
: Initializes the object with integersm
andk
and an empty stream.void addElement(int num)
: Adds the integernum
to the stream.int calculateMKAverage()
: Returns the current MK Average for the stream as described above, or-1
if the stream contains fewer thanm
elements. ...