- 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 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, 7] has average value of 6 and [10, 5, 6] has average of 7.

To solve this, we will follow these steps:

- target := target * k
- sum := 0, ans := 0
- for each index i and number n in nums, do
- if i >= k, then
- sum := sum - nums[i - k]

- sum := sum + n
- if i >=(k - 1) , then
- if sum >= target, then
- ans := ans + 1

- if sum >= target, then

- if i >= k, then
- return ans

Let us see the following implementation to get better understanding:

## Example Code

class Solution: def solve(self, nums, k, target): target *= k sum = 0 ans = 0 for i, n in enumerate(nums): if i >= k: sum -= nums[i - k] sum += n if i >= (k - 1): if sum >= target: ans += 1 return ans ob = Solution() nums = [1, 10, 5, 6, 7] k = 3 target = 6 print(ob.solve(nums, k, target))

## Input

[1, 10, 5, 6, 7], 3, 6

## Output

2

- Related Questions & Answers
- Program to find number of sublists whose sum is given target in python
- Program to find minimum number of subsequence whose concatenation is same as target in python
- Program to check we can find four elements whose sum is same as k or not in Python
- C++ code to find greater number whose factor is k
- Program to find number of unique subsequences same as target in C++
- Program to find maximum length of k ribbons of same length in Python
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find largest average of sublist whose size at least k in Python
- C++ program to find range whose sum is same as n
- Program to find array of length k from given array whose unfairness is minimum in python
- Python – Average of digit greater than K
- Program to find number of sublists that contains exactly k different words in Python
- Program to find max values of sublists of size k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to count number of paths whose sum is k in python

Advertisements