-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array Removals.cpp
59 lines (43 loc) · 952 Bytes
/
Array Removals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//{ Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int removals(vector<int>& arr, int k){
//Code here
sort(arr.begin(),arr.end());
int i=0;
int j=0;
int n=arr.size();
int ans=0;
while(j<n){
if(arr[j]-arr[i]<=k){
j++;
}else if(i<j){
i++;
}
ans=max(ans,j-i);
}
return n-ans;
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
Solution ob;
int ans = ob.removals(arr,k);
cout<<ans<<endl;
}
}
// } Driver Code Ends