Learner Level

Implement the movingAverageStreamLast method that returns the last moving average value for a fixed-size window.

The input contains an integer array nums, its length size, and a window size window. The values arrive like a stream from left to right.

After each new value, the moving average is calculated using at most the last window values. Your task is to return the final moving average after all values are processed.

The returned value should be the integer floor of the final average.

Example 1
Input:
nums (int[]) = [1,10,3,5]
size (int) = 4
window (int) = 3
Return:
(int) 6
Example 2
Input:
nums (int[]) = [1,2,3]
size (int) = 3
window (int) = 2
Return:
(int) 2
Example 3
Input:
nums (int[]) = [5]
size (int) = 1
window (int) = 3
Return:
(int) 5

Use a queue or sliding window sum.

Add each new value to the window and update the running sum. If the window contains more than the allowed number of values, remove the oldest value and subtract it from the sum.

After all values are processed, divide the current sum by the number of values currently in the window and return the integer result.

Pseudocode:

function movingAverageStreamLast(nums, size, window):
    queue = empty queue
    sum = 0
    for each value in nums:
        push value into queue
        sum = sum + value
        if size of queue > window:
            removed = pop front from queue
            sum = sum - removed
    if queue is empty:
        return 0
    return floor(sum / size of queue)
Run your code to see the result.