
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find largest average of sublist whose size at least k in Python
Suppose we have a list of numbers called nums and another value k, we have to find the largest average value of any sublist of the list whose length is at least k.
So, if the input is like nums = [2, 10, -50, 4, 6, 6] k = 3, then the output will be 5.33333333, as sublist [4, 6, 6] has the largest average value
To solve this, we will follow these steps −
left := minimum of nums, right := maximum of nums
s := sum of all numbers in nums from index 0 to k − 1
largest_avg := s / k
while left <= right, do
mid :=integer of (left + right) / 2
sum1 := s, avg := s / k, sum2 := 0, cnt := 0
for i in range k to size of nums, do
sum1 := sum1 + nums[i]
sum2 := sum2 + nums[i − k]
cnt := cnt + 1
avg := maximum of avg and (sum1 /(cnt + k))
if sum2 / cnt <= mid, then
sum1 := sum1 − sum2
cnt := 0, sum2 := 0
avg := maximum of avg and (sum1 /(cnt + k))
largest_avg := maximum of largest_avg and avg
if avg > mid, then
left := mid + 1
otherwise,
right := mid − 1
return largest_avg
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums, k): left, right = min(nums), max(nums) s = sum(nums[:k]) largest_avg = s / k while left <= right: mid = (left + right) // 2 sum1 = s avg = s / k sum2 = 0 cnt = 0 for i in range(k, len(nums)): sum1 += nums[i] sum2 += nums[i − k] cnt += 1 avg = max(avg, sum1 / (cnt + k)) if sum2 / cnt <= mid: sum1 −= sum2 cnt = 0 sum2 = 0 avg = max(avg, sum1 / (cnt + k)) largest_avg = max(largest_avg, avg) if avg > mid: left = mid + 1 else: right = mid − 1 return largest_avg ob = Solution() nums = [2, 10, −50, 4, 6, 6] k = 3 print(ob.solve(nums, k))
Input
[2, 10, −50, 4, 6, 6], k = 3
Output
5.333333333333333
- Related Articles
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find the sum of largest K sublist in Python
- Program to find a sub-list of size at least 2 whose sum is multiple of k in Python
- Program to find k where k elements have value at least k in Python
- Program to find sum of rectangle whose sum at most k in Python
- Program to find length of longest substring with character count of at least k in Python
- Program to find longest equivalent sublist after K increments in Python
- Largest sum subarray with at-least k numbers in C++
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to Find K-Largest Sum Pairs in Python
- Largest Number At Least Twice of Others in Python
- Program to find elements from list which have occurred at least k times in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
