- 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

# Remove One to Make Average k in Python

Suppose we have a list of numbers called nums and an integer k, we have to check whether we can remove exactly one element from the list to make the average equal to exactly k. Now we have to keep in mind that, there are some constraints −

- 2 ≤ n ≤ 1,000 where n is number of elements of nums list
- nums[i], k ≤ 1,000,000

So, if the input is like [5,3,2,4,6,10], k = 4, then the output will be True as if we remove 10, then the average of elements will be (5+3+2+4+6)/5 = 4, this is same as k.

To solve this, we will follow these steps −

- s:= total sum of all elements in nums
- t := k*(size of nums - 1)
- for each i in nums, do
- if s-i is same as t, then
- return True

- if s-i is same as t, then
- return False

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, nums, k): s=sum(nums) t = k*(len(nums)-1) for i in nums: if s-i == t: return True return False ob = Solution() nums = [5,3,2,4,6,10] k = 4 print(ob.solve(nums, k))

## Input

[5,3,2,4,6,10], 4

## Output

True

- Related Questions & Answers
- Python – Average of digit greater than K
- Remove Tuples of Length K in Python
- Remove Substrings in One Iteration in python
- Python – Remove characters greater than K
- Program to find largest average of sublist whose size at least k in Python
- Python – Remove Elements in K distance with N
- Remove K Digits in C++
- Program to find minimum remove required to make valid parentheses in Python
- Find maximum average subarray of k length in C++
- Python – Remove Tuples with difference greater than K
- Remove K Digits in C++ program
- How can I make one Python file run another?
- Program to find minimum one bit operations to make integers zero in Python
- Average of max K numbers in a stream in C++
- Minimum Remove to Make Valid Parentheses in C++

Advertisements