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