Count of subarrays of size K with average at least M | Function to count the subarrays of size K having average at least M ; Stores the resultant count of subarray ; Stores the sum of subarrays of size K ; Add the values of first K elements to the sum ; Increment the count if the current subarray is valid ; Traverse the given array ; Find the updated sum ; Check if current subarray is valid or not ; Return the count of subarrays ; Driver Code
def countSubArrays ( arr , N , K , M ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if sum >= K * M : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT sum += ( arr [ i ] - arr [ i - K ] ) NEW_LINE if sum >= K * M : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 3 , 2 , 1 , 3 , 9 ] NEW_LINE K = 2 NEW_LINE M = 4 NEW_LINE N = len ( arr ) NEW_LINE count = countSubArrays ( arr , N , K , M ) NEW_LINE print ( count ) NEW_LINE DEDENT