###### tags: `leetcode` # Question No.643: ### Description: Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. ### Solution: Sliding window and type casting. ### AC code ```cpp= class Solution { public: double findMaxAverage(vector<int>& nums, int k) { int len = nums.size(); double sum = 0; double cur_max = 0; for(int i = 0 ; i < k ; i++) sum+=nums[i]; cur_max = sum; for(int i = 1 ; i < len-k+1 ; i++){ sum-=nums[i-1]; sum+=nums[i+k-1]; if(sum>cur_max) cur_max = sum; } return cur_max/k; } }; ```