fork download
  1.  
  2.  
  3.  
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7. class HACKRNDM
  8. {
  9. private static Reader in;
  10. private static PrintWriter out;
  11.  
  12. public static void main(String args[])
  13. throws IOException
  14. {
  15. in = new Reader ();
  16. out = new PrintWriter (System.out, true);
  17.  
  18. int n=in.nextInt();
  19. int k=in.nextInt();
  20.  
  21. int pos=0;
  22. int arr[]=new int[n];
  23. ArrayList<Integer> list=new ArrayList<Integer>();
  24.  
  25. int max=Integer.MIN_VALUE;
  26. int min=Integer.MAX_VALUE;
  27. for(int i=0;i<n;i++)
  28. {
  29. int ele=in.nextInt();
  30.  
  31. arr[pos++]=ele;
  32. list.add(arr[pos-1]);
  33. }
  34.  
  35.  
  36.  
  37. int counter=0;
  38. ListIterator<Integer> it = list.listIterator();
  39. while(it.hasNext())
  40. {
  41. int ele= it.next();
  42. if(list.contains(ele+k))
  43. counter++;
  44. if(list.contains(ele-k))
  45. counter++;
  46.  
  47. it.remove();
  48. }
  49.  
  50. System.out.println(counter);
  51. out.flush();
  52. out.close();
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. class Reader
  61. {
  62. final private int BUFFER_SIZE = 1 << 16;
  63.  
  64. private DataInputStream din;
  65. private byte[] buffer;
  66. private int bufferPointer, bytesRead;
  67.  
  68. public Reader()
  69. {
  70. din = new DataInputStream(System.in);
  71. buffer = new byte[BUFFER_SIZE];
  72. bufferPointer = bytesRead = 0;
  73. }
  74.  
  75. public Reader(String file_name) throws IOException
  76. {
  77. din = new DataInputStream(new FileInputStream(file_name));
  78. buffer = new byte[BUFFER_SIZE];
  79. bufferPointer = bytesRead = 0;
  80. }
  81.  
  82. public String readLine() throws IOException
  83. {
  84. byte[] buf = new byte[64]; // line length
  85. int cnt = 0, c;
  86. while( (c=read()) != -1) {
  87. buf[cnt++] = (byte)c;
  88. if(c == '\n') break;
  89. }
  90. return new String(buf,0,cnt);
  91. }
  92.  
  93. public int nextInt() throws IOException
  94. {
  95. int ret = 0;
  96. byte c = read();
  97. while (c <= ' ') c = read();
  98. boolean neg = c == '-';
  99. if (neg) c = read();
  100. do {
  101. ret = ret * 10 + c - '0';
  102. c = read();
  103. } while (c >= '0' && c <= '9');
  104. if (neg) return -ret;
  105. return ret;
  106. }
  107.  
  108. public long nextLong() throws IOException
  109. {
  110. long ret = 0;
  111. byte c = read();
  112. while (c <= ' ') c = read();
  113. boolean neg = c == '-';
  114. if (neg) c = read();
  115. do {
  116. ret = ret * 10 + c - '0';
  117. c = read();
  118. } while (c >= '0' && c <= '9');
  119. if (neg) return -ret;
  120. return ret;
  121. }
  122.  
  123. public double nextDouble() throws IOException
  124. {
  125. double ret = 0, div = 1;
  126. byte c = read();
  127. while(c <= ' ') c = read();
  128. boolean neg = c == '-';
  129. if(neg) c = read();
  130. do {
  131. ret = ret * 10 + c - '0';
  132. c = read();
  133. } while (c >= '0' && c <= '9');
  134. if(c == '.')
  135. while((c=read()) >= '0' && c <= '9') {
  136. div *= 10;
  137. ret = ret + (c - '0') / div;
  138. }
  139. if (neg) return -ret;
  140. return ret;
  141. }
  142.  
  143. private void fillBuffer() throws IOException
  144. {
  145. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  146. if (bytesRead == -1) buffer[0] = -1;
  147. }
  148.  
  149. private byte read() throws IOException
  150. {
  151. if (bufferPointer == bytesRead) fillBuffer();
  152. return buffer[bufferPointer++];
  153. }
  154.  
  155. public void close() throws IOException
  156. {
  157. if(din == null) return;
  158. din.close();
  159. }
  160. }
Success #stdin #stdout 0.06s 380224KB
stdin
5 2
1 5 3 4 2
stdout
3