class MovingAverage { Queue<Integer> queue = new LinkedList<>(); int size = 0; double sum=0; public MovingAverage(int size) { this.size = size; } public double next(int val) { if(queue.size()==size){ sum-=queue.poll(); } queue.offer(val); sum+=val; return sum/queue.size(); } }