human
Find maximum average subarray of k length | Returns beginning index of maximum average subarray of length k.
Check if ' k ' is valid.
Compute sum of first ' k ' elements.
Compute sum of remaining subarrays.
Return starting index.
Driver program"
How can the above be solved in Python?
 Write me a code to find maximum average subarray of k length and the other steps required?
assistant
#Here is code to find maximum average subarray of k length and the other steps required.
 def findMaxAverage ( arr , n , k ) :
	if ( k > n ) :
		return - 1
	sum = arr [ 0 ]
	for i in range ( 1 , k ) :
		sum += arr [ i ]
	max_sum = sum
	max_end = k - 1
	for i in range ( k , n ) :
		sum = sum + arr [ i ] - arr [ i - k ]
		if ( sum > max_sum ) :
			max_sum = sum
			max_end = i
	return max_end - k + 1
arr = [ 1 , 12 , - 5 , - 6 , 50 , 3 ]
k = 4
n = len ( arr )
print ( " The ▁ maximum ▁ average ▁ subarray ▁ of ▁ length " , k , " begins ▁ at ▁ index " , findMaxAverage ( arr , n , k ) )