Difference between maximum and minimum average of all K | Function to find the difference between averages of the maximum and the minimum subarrays of length k ; Stores min and max sum ; Iterate through starting points ; Sum up next K elements ; Update max and min moving sum ; Return the difference between max and min average ; Given Input ; Function Call
def Avgdifference ( arr , N , K ) : NEW_LINE INDENT min = 1000000 ; NEW_LINE max = - 1 ; NEW_LINE for i in range ( N - K + 1 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for j in range ( K ) : NEW_LINE INDENT sum += arr [ i + j ] ; NEW_LINE DEDENT if ( min > sum ) : NEW_LINE INDENT min = sum ; NEW_LINE DEDENT if ( max < sum ) : NEW_LINE INDENT max = sum ; NEW_LINE DEDENT DEDENT return ( max - min ) / K ; NEW_LINE DEDENT arr = [ 3 , 8 , 9 , 15 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 2 ; NEW_LINE print ( Avgdifference ( arr , N , K ) ) ; NEW_LINE