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++
#include <iostream>
using namespace std;
int countSubArrays( int arr[], int N,
int K, int M)
{
int count = 0;
int sum = 0;
for ( int i = 0; i < K; i++) {
sum += arr[i];
}
if (sum >= K * M)
count++;
for ( int i = K; i < N; i++) {
sum += (arr[i] - arr[i - K]);
if (sum >= K * M)
count++;
}
return count;
}
int 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;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int [] arr = { 3 , 6 , 3 , 2 , 1 , 3 , 9 };
int K = 2 , M = 4 ;
System.out.println(countSubArrays(arr, K, M));
}
public static int countSubArrays( int [] arr, int K,
int M)
{
int count = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < K; i++) {
sum += arr[i];
}
if (sum >= K * M)
count++;
for ( int i = K; i < arr.length; i++) {
sum += (arr[i] - arr[i - K]);
if (sum >= K * M)
count++;
}
return count;
}
}
|
Python3
def countSubArrays(arr, N, K, M):
count = 0
sum = 0
for i in range (K):
sum + = arr[i]
if sum > = K * M:
count + = 1
for i in range (K, N):
sum + = (arr[i] - arr[i - K])
if sum > = K * M:
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 3 , 6 , 3 , 2 , 1 , 3 , 9 ]
K = 2
M = 4
N = len (arr)
count = countSubArrays(arr, N, K, M)
print (count)
|
C#
using System;
public class GFG
{
public static void Main(String[] args)
{
int [] arr = { 3, 6, 3, 2, 1, 3, 9 };
int K = 2, M = 4;
Console.WriteLine(countSubArrays(arr, K, M));
}
public static int countSubArrays( int [] arr, int K,
int M)
{
int count = 0;
int sum = 0;
for ( int i = 0; i < K; i++) {
sum += arr[i];
}
if (sum >= K * M)
count++;
for ( int i = K; i < arr.Length; i++) {
sum += (arr[i] - arr[i - K]);
if (sum >= K * M)
count++;
}
return count;
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...