Idea is : avg[i] = max( (avg[i - 1] * len[i - 1] + nums[i]) / (len[i - 1] + 1), sum /k);

Where sum is the running sum which keeps sum of last k elements. avg[i] represents max average of the array which ends at ith index. len[i] keeps track of how many numbers used to compute the max average that ends at ith index.

```
public class Solution {
public double findMaxAverage(int[] nums, int k) {
double sum = 0, maxAvg = Integer.MIN_VALUE;
double[] sums = new double[nums.length];
int[] lens = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (i >= k) {
sum -= nums[i - k];
double prev = (sums[i - 1] + nums[i]) / (lens[i - 1] + 1);
double curr = sum / k;
if (prev > curr) {
sums[i] = sums[i - 1] + nums[i];
lens[i] = lens[i - 1] + 1;
} else {
sums[i] = sum;
lens[i] = k;
}
maxAvg = Math.max(maxAvg, sums[i]/lens[i]);
}
if (i + 1 == k) {
sums[i] = sum;
lens[i] = k;
maxAvg = sum/k;
}
}
return maxAvg;
}
}
```