Source Solution : class Solution { public double findMaxAverage(int[] nums, int k) { if (k > nums.length) return 0.0; int begin = 0, end = 0; double sum = 0.0; double maxAverage = Double.NEGATIVE_INFINITY; while (end < nums.length) { sum += nums[end]; //As soon as the window size is k find the avg and store in Max Average. if (end - begin + 1 == k) { double tempAvg = sum/k; maxAverage = Math.max(maxAverage, tempAvg); sum -= nums[begin]; begin++; } end++; } return maxAverage; } }