1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode Solution
In this guide, you will get 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode Solution with the best time and space complexity. The solution to Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold solution in C++
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold solution in Java
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold solution in Python
- Additional Resources

Problem Statement of Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold.
Example 1:
Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3
Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
Example 2:
Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
Output: 6
Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
Constraints:
1 <= arr.length <= 105
1 <= arr[i] <= 104
1 <= k <= arr.length
0 <= threshold <= 104
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode Solution in C++
class Solution {
public:
int numOfSubarrays(vector<int>& arr, int k, int threshold) {
int ans = 0;
int windowSum = 0;
for (int i = 0; i < arr.size(); ++i) {
windowSum += arr[i];
if (i >= k)
windowSum -= arr[i - k];
if (i >= k - 1 && windowSum / k >= threshold)
++ans;
}
return ans;
}
};
/* code provided by PROGIEZ */
1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode Solution in Java
class Solution {
public int numOfSubarrays(int[] arr, int k, int threshold) {
int ans = 0;
int windowSum = 0;
for (int i = 0; i < arr.length; ++i) {
windowSum += arr[i];
if (i >= k)
windowSum -= arr[i - k];
if (i >= k - 1 && windowSum / k >= threshold)
++ans;
}
return ans;
}
}
// code provided by PROGIEZ
1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold LeetCode Solution in Python
class Solution:
def numOfSubarrays(self, arr: list[int], k: int, threshold: int) -> int:
ans = 0
windowSum = 0
for i in range(len(arr)):
windowSum += arr[i]
if i >= k:
windowSum -= arr[i - k]
if i >= k - 1 and windowSum // k >= threshold:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.