human
Find the subarray with least average | Prints beginning and ending indexes of subarray of size k with minimum average.
k must be smaller than or equal to n.
Initialize beginning index of result.
Compute sum of first subarray of size k.
Initialize minimum sum as current sum.
Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element.
Add current item and remove first item of previous subarray.
Update result if needed.
Driver Code.
Subarray size"
How can the above be solved in Python?
assistant
#Here is code to find the subarray with least average and the other steps required.
 def findMinAvgSubarray ( arr , n , k ) :
	if ( n < k ) : return 0
	res_index = 0
	curr_sum = 0
	for i in range ( k ) :
		curr_sum += arr [ i ]
	min_sum = curr_sum
	for i in range ( k , n ) :
		curr_sum += arr [ i ] - arr [ i - k ]
		if ( curr_sum < min_sum ) :
			min_sum = curr_sum
			res_index = ( i - k + 1 )
	print ( " Subarray ▁ between ▁ [ " , res_index , " , ▁ " , ( res_index + k - 1 ) , " ] ▁ has ▁ minimum ▁ average " )
arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ]
k = 3
n = len ( arr )
findMinAvgSubarray ( arr , n , k )