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
function countSubArrays ( arr , N , K , M ) { let count = 0 ; let sum = 0 ; for ( let i = 0 ; i < K ; i ++ ) { sum += arr [ i ] ; } if ( sum >= K * M ) count ++ ; for ( let i = K ; i < N ; i ++ ) { sum += ( arr [ i ] - arr [ i - K ] ) ; if ( sum >= K * M ) count ++ ; } return count ; } let arr = [ 3 , 6 , 3 , 2 , 1 , 3 , 9 ] ; let K = 2 , M = 4 ; let N = arr . length ; document . write ( countSubArrays ( arr , N , K , M ) ) ;