Minimum Removals
You have been given an array/list "ARR" consisting of 'N' integers. You have also given an integer 'K'.
Your task is to find the minimum number of elements that should be removed from "ARR" (possibly zero) such that the difference between the maximum element and the minimum element of the remaining "ARR" is less than or equal to 'K', i.e. ARRmax - ARRmin <= K.
Note :
1. "ARR" can contain duplicates.
For Example :
Input: 'N' = 4 , "ARR" = [5, 10 , 2] and 'K' = 3.
Output: 1
Explanation : Currently, the difference between the maximum and minimum element in the array is 10 - 2 = 8, which is greater than K (3).
So, we need to remove some elements. The optimal way to get our result is to remove 10. After removing 10, the difference between maximum and minimum is 5 - 2 = 3, which is less than or equal to K.
Input Format
The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two single-space separated integers ‘N’ and ‘K’, respectively.
The second line of each test case contains ‘N’ single space-separated integers, denoting the elements of the array/list.
Output Format :
Print the minimum number of elements that should be removed such that the difference between the maximum element and the minimum element of the remaining array is less than or equal to K.
Print the output of each test case in a separate line.
Note: You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 5000
0 <= K <= 10^5
-10^5 <= ARR[i] <=10^5
Where 'N' denotes the number of elements in the given array/list, 'K' is the given integer and ARR[i] denotes the i-th element of the given array/list.
Time Limit : 1 second
Be the first one to answer
Add answer anonymously...
Top RUBRIK INDIA Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
RUBRIK INDIA Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app