- 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

Suppose we have a lit of numbers called nums, and have another value k. If we remove k elements from nums, then find the minimum of (maximum of nums - minimum of nums).

So, if the input is like nums = [4, 10, 3, 2, 8, 9] k = 3, then the output will be 2, because if we remove 10, 8 and 9 then maximum is 4, minimum is 2 so difference is 2.

To solve this, we will follow these steps −

sort the list nums

p := size of nums - k

m := (last element of nums) - nums[0]

for i in range 0 to size of nums - p, do

if nums[i + p - 1] - nums[i] < m, then

m := nums[i + p - 1] - nums[i]

return m

Let us see the following implementation to get better understanding

def solve(nums, k): nums = sorted(nums) p = len(nums) - k m = nums[-1] - nums[0] for i in range(0, len(nums) - p + 1): if nums[i + p - 1] - nums[i] < m: m = nums[i + p - 1] - nums[i] return m nums = [10, 4, 3, 2, 9, 8] k = 3 print(solve(nums, k))

[10, 4, 3, 2, 9, 8], 3

2

- Related Questions & Answers
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Find the k smallest numbers after deleting given elements in C++
- Program to find minimum length of string after deleting similar ends in Python
- Find all possible substrings after deleting k characters in Python
- Find the k largest numbers after deleting the given elements in C++
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find minimum possible maximum value after k operations in python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Find the smallest after deleting given elements using C++
- Find the largest after deleting the given elements in C++
- Program to maximize the minimum value after increasing K sublists in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Python program to find Non-K distant elements
- Program to find k partitions after truncating sentence using Python

Advertisements