Advertisement
Spocoman

10. Pairs by Difference

Oct 27th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int[] numbers = Arrays
  8.                 .stream(scanner.nextLine().split(" "))
  9.                 .mapToInt(Integer::parseInt)
  10.                 .toArray();
  11.        
  12.         int difference = Integer.parseInt(scanner.nextLine()),
  13.                 count = 0;
  14.        
  15.         for (int i = 0; i < numbers.length; i++) {
  16.             for (int j = i; j < numbers.length; j++) {
  17.                 if (Math.abs(numbers[i] - numbers[j]) == difference) {
  18.                     count++;
  19.                 }
  20.             }
  21.         }
  22.  
  23.         System.out.println(count);
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement