Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold - Complete Solution Guide
Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold is LeetCode problem 1343, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 fir
Detailed Explanation
The problem asks us to find the number of sub-arrays of a given array `arr` that meet two criteria: (1) they have a length of `k`, and (2) their average is greater than or equal to a given `threshold`. The input consists of the array `arr` of integers, the length of the sub-array `k`, and the minimum average `threshold`. The output is the count of sub-arrays meeting these conditions.
Solution Approach
The provided solution employs a sliding window technique to efficiently calculate the sum of each sub-array of length `k`. First, it calculates the sum of the first `k` elements. Then, it iterates through the rest of the array, subtracting the leftmost element of the current window and adding the next element on the right to update the sum. After each update, it checks if the sum is greater than or equal to `k * threshold` and increments the count if it is.
Step-by-Step Algorithm
- Step 1: Calculate `target_sum` by multiplying `k` by `threshold`. This avoids repeatedly calculating the average.
- Step 2: Initialize a variable `count` to 0 to store the number of sub-arrays that meet the condition.
- Step 3: Calculate the sum of the first `k` elements of the array `arr` and store it in `current_sum`.
- Step 4: Check if `current_sum` is greater than or equal to `target_sum`. If it is, increment `count`.
- Step 5: Iterate from `i = k` to the end of the array. In each iteration, update `current_sum` by subtracting `arr[i-k]` (the element leaving the window) and adding `arr[i]` (the element entering the window).
- Step 6: Check if the updated `current_sum` is greater than or equal to `target_sum`. If it is, increment `count`.
- Step 7: Return the final value of `count`.
Key Insights
- Insight 1: The key is to avoid recalculating the sum of each sub-array from scratch. Use a sliding window technique to efficiently update the sum as we move across the array.
- Insight 2: Multiplying the threshold by `k` allows us to work with the sum directly instead of repeatedly calculating the average, which can improve efficiency.
- Insight 3: The problem constraints indicate that brute-force solutions might not be efficient enough due to potentially large input array sizes. Optimization is required.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Sliding Window.
Companies
Asked at: Turo.