Count Fixed-Length Windows Meeting an Average Threshold

Quick Overview

Count Fixed-Length Windows Meeting an Average Threshold. Return the number of contiguous subarrays of exactly k elements whose arithmetic mean is at least threshold.

Count Fixed-Length Windows Meeting an Average Threshold

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the number of contiguous subarrays of exactly k elements whose arithmetic mean is at least threshold. Averages need not be integers. ```cpp class Solution { public: int numOfSubarrays(vector<int>& arr, int k, int threshold); }; ``` ### Examples **Example 1:** ```text Input: arr = [1,5,3,7], k = 2, threshold = 4 Output: 2 Explanation: Window sums are6,8,10. The last two meet the required sum8. ``` **Example 2:** ```text Input: arr = [1,2], k = 2, threshold = 2 Output: 0 Explanation: The sole average is1.5, below2. ``` ### Constraints - arr has 1 to 100000 integers, each in `[1,10000]`. - `1 <= k <= arr.length`. - threshold is an integer in `[0,10000]`. ### Contract considerations The window length is fixed at k, and threshold may be 0. ```hint Read the boundary condition Check equality at the average threshold and windows covering the whole array. Every counted window has exactly the requested length. ``` ### Boundary reminders The window length is fixed at k, and threshold may be 0.

Overview: Count Fixed-Length Windows Meeting an Average Threshold. Return the number of contiguous subarrays of exactly k elements whose arithmetic mean is at least threshold.

Amazon logo
Amazon
Jun 11, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the number of contiguous subarrays of exactly k elements whose arithmetic mean is at least threshold. Averages need not be integers.

class Solution {
public:
    int numOfSubarrays(vector<int>& arr, int k, int threshold);
};

Examples

Example 1:

Input: arr = [1,5,3,7], k = 2, threshold = 4
Output: 2
Explanation: Window sums are6,8,10. The last two meet the required sum8.

Example 2:

Input: arr = [1,2], k = 2, threshold = 2
Output: 0
Explanation: The sole average is1.5, below2.

Constraints

  • arr has 1 to 100000 integers, each in [1,10000] .
  • 1 <= k <= arr.length .
  • threshold is an integer in [0,10000] .

Contract considerations

The window length is fixed at k, and threshold may be 0.

Boundary reminders

The window length is fixed at k, and threshold may be 0.