Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class type {
- public static void main(String[] args) throws IOException {
- BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
- String[] input = b.readLine().split(" ");
- int n = Integer.parseInt(input[0]);
- int d = Integer.parseInt(input[1]);
- int[] sticks = new int[n];
- for (int i = 0; i < n; i++) {
- sticks[i] = Integer.parseInt(b.readLine());
- }
- sort(sticks);
- int result = solve(sticks, d);
- System.out.println(result);
- }
- private static void sort(int[] a) {
- // TODO Auto-generated method stub
- for (int i = 0; i < a.length - 1; i++) {
- for (int j = i + 1; j < a.length; j++) {
- if (a[i] > a[j]) {
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- }
- }
- }
- }
- private static int solve(int[] sticks, int d) {
- // TODO Auto-generated method stub
- int numPair = 0;
- for (int i = 0; i < sticks.length - 1;) {
- if (sticks[i] >= sticks[i + 1] - d) {
- numPair++;
- i += 2;
- } else {
- i += 1;
- }
- }
- return numPair;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement