Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of K-Length sublists whose average is greater or same as target in python
Suppose we have a list nums, and two additional values k and target, we have to find the number of sublists whose size is k and its average value ? target.
So, if the input is like nums = [1, 10, 5, 6, 7] k = 3 target = 6, then the output will be 2, as the sublist [1, 10, 5] has average value of 5.33 (less than 6), [10, 5, 6] has average of 7, and [5, 6, 7] has average of 6.
Algorithm
To solve this efficiently using a sliding window approach, we will follow these steps ?
- Multiply target by k to avoid division in comparisons
- Use sliding window technique to calculate sum of each k-length sublist
- Compare sum with target×k instead of average with target
- Count sublists that meet the criteria
Example
Let's implement the solution using the sliding window technique ?
def count_sublists_with_target_average(nums, k, target):
target_sum = target * k
current_sum = 0
count = 0
for i, num in enumerate(nums):
# Remove the leftmost element if window size exceeds k
if i >= k:
current_sum -= nums[i - k]
# Add current element to window
current_sum += num
# Check if we have a complete window of size k
if i >= (k - 1):
if current_sum >= target_sum:
count += 1
return count
# Test the function
nums = [1, 10, 5, 6, 7]
k = 3
target = 6
result = count_sublists_with_target_average(nums, k, target)
print(f"Number of {k}-length sublists with average >= {target}: {result}")
Number of 3-length sublists with average >= 6: 2
How It Works
The algorithm uses a sliding window approach to efficiently calculate sums ?
- Window sliding: Instead of recalculating the sum for each sublist, we slide the window by removing the leftmost element and adding the new rightmost element
- Avoiding division: By multiplying target by k, we compare sums directly instead of calculating averages
- Time complexity: O(n) where n is the length of the input list
Step-by-Step Execution
Let's trace through the example to understand better ?
nums = [1, 10, 5, 6, 7]
k = 3
target = 6
# Sublists of length 3 and their averages:
sublist_1 = [1, 10, 5] # sum = 16, average = 16/3 = 5.33 (< 6)
sublist_2 = [10, 5, 6] # sum = 21, average = 21/3 = 7 (>= 6) ?
sublist_3 = [5, 6, 7] # sum = 18, average = 18/3 = 6 (>= 6) ?
print("Sublist analysis:")
nums = [1, 10, 5, 6, 7]
k = 3
target = 6
for i in range(len(nums) - k + 1):
sublist = nums[i:i+k]
avg = sum(sublist) / k
status = "?" if avg >= target else "?"
print(f"{sublist}: sum={sum(sublist)}, avg={avg:.2f} {status}")
Sublist analysis: [1, 10, 5]: sum=16, avg=5.33 ? [10, 5, 6]: sum=21, avg=7.00 ? [5, 6, 7]: sum=18, avg=6.00 ?
Conclusion
The sliding window technique provides an efficient O(n) solution for finding k-length sublists with average ? target. By multiplying the target by k, we avoid floating-point division and work with integer comparisons for better performance.
