

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find a sub-list of size at least 2 whose sum is multiple of k in Python
- Program to find the sum of largest K sublist 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
- Largest sum subarray with at-least k numbers in C++
- Largest Number At Least Twice of Others in Python
- Program to find length of longest substring with character count of at least k in Python
- Maximum average of a subarray of size of at least X and at most Y in C++
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find elements from list which have occurred at least k times in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to Find K-Largest Sum Pairs in Python
- Program to find longest equivalent sublist after K increments in Python