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