#C1469. Moving Average from Data Stream
Moving Average from Data Stream
Moving Average from Data Stream
You are given a stream of integers and a window size \( k \). Your task is to implement a data structure that calculates the moving average of the last \( k \) integers received. The moving average is defined as:
[ \text{average} = \frac{a_1 + a_2 + \cdots + a_k}{k} ]
There are two operations:
1 x
: Append integer \( x \) to the stream and immediately output the moving average of the last \( k \) numbers.2
: Output the current moving average of the last \( k \) numbers without adding a new element.
Note: If the total numbers seen so far is less than \( k \), the moving average is computed on all the numbers received so far.
inputFormat
The first line contains a single integer \( k \) indicating the size of the moving window.
The second line contains an integer \( n \) that represents the number of operations.
Each of the following \( n \) lines describes an operation in one of the following formats:
1 x
: Call thenext(x)
operation. Print the moving average after this insertion.2
: Call thegetAverage()
operation. Print the current moving average.
outputFormat
For each operation, output a line containing the resulting moving average as a floating point number.
## sample3
5
1 1
1 10
1 3
1 5
2
1.0
5.5
4.666666666666667
6.0
6.0
</p>