Count of subarrays of size K with average at least M
Given an array arr[] consisting of N integers and two positive integers K and M, the task is to find the number of subarrays of size K whose average is at least M.
Examples:
Input: arr[] = {2, 3, 3, 4, 4, 4, 5, 6, 6}, K = 3, M = 4
Output: 4
Explanation:
Below are the subarrays of size K(= 3) whose average is at least M(= 4) as:
- arr[3, 5]: The average is 4 which is at least M(= 4).
- arr[4, 6]: The average is 4.33 which is at least M(= 4).
- arr[5, 7]: The average is 5 which is at least M(= 4).
- arr[6, 8]: The average is 5.66 which is at least M(= 4).
Therefore, the count of the subarray is given by 4.
Input: arr[] = {3, 6, 3, 2, 1, 3, 9] K = 2, M = 4
Output: 3
Approach: The given problem can be solved by using the Two Pointers and Sliding Window Technique. Follow the steps below to solve the given problem:
- Initialize a variable, say count as 0 that stores the count of all possible subarrays.
- Initialize a variable, say sum as 0 that stores the sum of elements of the subarray of size K.
- Find the sum of the first K array elements and store it in the variable sum. If the value of sum is at least M*K, then increment the value of count by 1.
- Traverse the given array arr[] over the range [K, N – 1] using the variable i and perform the following steps:
- Add the value of arr[i] to the variable sum and subtract the value of arr[i – K] from the sum.
- If the value of sum is at least M*K, then increment the value of count by 1.
- After completing the above steps, print the value of count as the resultant count of subarrays.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <iostream>using namespace std; // Function to count the subarrays of// size K having average at least Mint countSubArrays(int arr[], int N, int K, int M){ // Stores the resultant count of // subarray int count = 0; // Stores the sum of subarrays of // size K int sum = 0; // Add the values of first K elements // to the sum for (int i = 0; i < K; i++) { sum += arr[i]; } // Increment the count if the // current subarray is valid if (sum >= K * M) count++; // Traverse the given array for (int i = K; i < N; i++) { // Find the updated sum sum += (arr[i] - arr[i - K]); // Check if current subarray // is valid or not if (sum >= K * M) count++; } // Return the count of subarrays return count;} // Driver Codeint main(){ int arr[] = { 3, 6, 3, 2, 1, 3, 9 }; int K = 2, M = 4; int N = sizeof(arr) / sizeof(arr[0]); cout << countSubArrays(arr, N, K, M); return 0;} |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


