-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray Removals.java
More file actions
48 lines (40 loc) · 1.22 KB
/
Array Removals.java
File metadata and controls
48 lines (40 loc) · 1.22 KB
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
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while (tc-- > 0) {
String[] inputLine;
inputLine = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int k = Integer.parseInt(inputLine[1]);
int[] arr = new int[n];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
int ans = new Solution().removals(arr, n, k);
System.out.println(ans);
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution {
int removals(int[] arr, int n, int k) {
// code here
Arrays.sort(arr);
int i=0;
int j=0;
int maxSize=0;
while(j<n){
if(arr[j]-arr[i]<=k)j++;
else if(i<j)i++;
maxSize=Math.max(maxSize,j-i);
}
return n-maxSize;
}
}