Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

The task is to find the count of subarrays within an integer array 'arr', each of size 'k', where the average of elements in the subarray is greater than or equal to 'threshold'. 


Example 1:

   Input: arr = [2,4,6,8,10], k = 2, threshold = 5

   Output: 3

   Explanation: Subarrays [4,6], [6,8], and [8,10] have averages of 5, 7, and 9 respectively, meeting the threshold condition.



Example 2:

 Input: arr = [1,2,3,4,5,6,7,8], k = 4, threshold = 6

   Output: 3

   Explanation: Subarrays [4,5,6,7], [5,6,7,8], and [6,7,8,9] have averages of 6.5, 6.5, and 7.5 respectively, all exceeding the threshold of 6.




Java implementation:

    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int n = arr.length;
        int left = 0;
        int right = 0;
        int sum = 0;
        int count = 0;
        while(right<n){
            sum = sum + arr[right];
            if(right-left+1 == k){
                if((sum/k)>=threshold){
                    count++;
                }
            }else if(right-left+1>k){
                sum = sum - arr[left];
                if((sum/k)>=threshold){
                    count++;
                }
                left++;
            }
            right++;
        }
        return count;
    }







C++ implementation:

#include <vector>
using namespace std;

int numOfSubarrays(vector<int>& arr, int k, int threshold) {
    int n = arr.size();
    int left = 0;
    int right = 0;
    int sum = 0;
    int count = 0;
    
    while (right < n) {
        sum = sum + arr[right];
        if (right - left + 1 == k) {
            if ((sum / k) >= threshold) {
                count++;
            }
        } else if (right - left + 1 > k) {
            sum = sum - arr[left];
            if ((sum / k) >= threshold) {
                count++;
            }
            left++;
        }
        right++;
    }
    
    return count;
}







Python implementation:

def numOfSubarrays(arr, k, threshold):
    n = len(arr)
    left = 0
    right = 0
    total_sum = 0
    count = 0
    
    while right < n:
        total_sum += arr[right]
        if right - left + 1 == k:
            if total_sum / k >= threshold:
                count += 1
        elif right - left + 1 > k:
            total_sum -= arr[left]
            if total_sum / k >= threshold:
                count += 1
            left += 1
        right += 1
    
    return count





Here's how the method works:
1. It initializes variables `left`, `right`, `sum`, and `count`. 
   - `left` and `right` are pointers used for the sliding window technique.
   - `sum` stores the sum of elements within the current window.
   - `count` keeps track of the count of subarrays meeting the conditions.

2. It iterates through the array using a sliding window approach. At each step:
   - It adds the value at index `right` to the `sum`.
   - If the size of the current window (`right - left + 1`) is equal to `k`:
     - It checks if the average of elements within the window (computed as `sum / k`) is greater than or equal to `threshold`. If so, it increments the `count`.
   - If the size of the window exceeds `k`:
     - It subtracts the value at index `left` from the `sum` and increments `left`.
     - Then it checks if the average of elements within the adjusted window is greater than or equal to `threshold`. If so, it increments the `count`.
   
3. The method continues until `right` reaches the end of the array.
4. Finally, it returns the `count` representing the number of subarrays meeting the conditions.

This method efficiently solves the problem by using the sliding window technique to calculate the sum of elements within each subarray and then checking the average against the given threshold.






No comments

darkmode