fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. public static int countPairs(int[] b, int k) {
  6. int count = 0;
  7. int n = b.length;
  8. for (int i = 0; i < n; ++i) {
  9. for (int j = i+1; j < n; ++j) {
  10. if (Math.abs(b[i] - b[j]) == k) {
  11. count++;
  12. }
  13. }
  14. }
  15. return count;
  16. }
  17.  
  18. public static void main(String[] args) {
  19. int[] b = {1, 7, 5, 9, 2, 12, 3};
  20. int k = 2;
  21. System.out.println(countPairs(b, k));
  22. }
  23. }
  24.  
Success #stdin #stdout 0.08s 52612KB
stdin
Standard input is empty
stdout
4