|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class Problem11Pairs { // O(N * log(N)) (linear) solution for counting (without the printing) |
| 5 | + public static void main(String... args) { |
| 6 | + Scanner sc = new Scanner(System.in); |
| 7 | + |
| 8 | + System.out.print("Target sum: "); |
| 9 | + int target = Integer.parseInt(sc.nextLine()); |
| 10 | + |
| 11 | + System.out.print("Integers: "); |
| 12 | + String[] input = sc.nextLine().split(" "); |
| 13 | + final int N = input.length; |
| 14 | + |
| 15 | + int[] arr = new int[N]; |
| 16 | + |
| 17 | + for (int i = 0; i < N; i++) { |
| 18 | + arr[i] = Integer.parseInt(input[i]); |
| 19 | + } |
| 20 | + |
| 21 | + Arrays.sort(arr); |
| 22 | + |
| 23 | + long pairs = 0; |
| 24 | + int[] uniques = new int[N]; |
| 25 | + int[] repetitions = new int[N]; |
| 26 | + |
| 27 | + int k = 0; |
| 28 | + int rep = 1; |
| 29 | + for (int i = 1; i < N; i++) { |
| 30 | + if (arr[i] == arr[i - 1]) { |
| 31 | + rep++; |
| 32 | + if (i == N - 1) { |
| 33 | + uniques[k] = arr[i]; |
| 34 | + repetitions[k] = rep; |
| 35 | + k++; |
| 36 | + } |
| 37 | + } else { |
| 38 | + uniques[k] = arr[i - 1]; |
| 39 | + repetitions[k] = rep; |
| 40 | + k++; |
| 41 | + rep = 1; |
| 42 | + if (i == N - 1) { |
| 43 | + uniques[k] = arr[i]; |
| 44 | + repetitions[k] = 1; |
| 45 | + k++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + int l = 0; |
| 51 | + int r = k - 1; |
| 52 | + if (l == r) { |
| 53 | + if (uniques[l] * 2 == target) { |
| 54 | + int combinations = repetitions[l] * (repetitions[l] - 1) / 2; |
| 55 | + pairs += combinations; |
| 56 | +// for (int i = 0; i < combinations; i++) { |
| 57 | +// System.out.printf("%d, %d%n", uniques[l], uniques[l]); |
| 58 | +// } |
| 59 | + } |
| 60 | + } else { |
| 61 | + while (l < r) { |
| 62 | + if (uniques[l] * 2 == target) { |
| 63 | + int combinations = repetitions[l] * (repetitions[l] - 1) / 2; |
| 64 | + pairs += combinations; |
| 65 | +// for (int i = 0; i < combinations; i++) { |
| 66 | +// System.out.printf("%d, %d%n", uniques[l], uniques[l]); |
| 67 | +// } |
| 68 | + } |
| 69 | + |
| 70 | + if (uniques[r] * 2 == target) { |
| 71 | + int combinations = repetitions[r] * (repetitions[r] - 1) / 2; |
| 72 | + pairs += combinations; |
| 73 | +// for (int i = 0; i < combinations; i++) { |
| 74 | +// System.out.printf("%d, %d%n", uniques[r], uniques[r]); |
| 75 | +// } |
| 76 | + } |
| 77 | + |
| 78 | + int currentCombination = uniques[l] + uniques[r]; |
| 79 | + if (currentCombination == target) { |
| 80 | + int combinations = repetitions[l] * repetitions[r]; |
| 81 | + pairs += combinations; |
| 82 | +// for (int i = 0; i < combinations; i++) { |
| 83 | +// System.out.printf("%d, %d%n", uniques[l], uniques[r]); |
| 84 | +// } |
| 85 | + l++; |
| 86 | + } else if (currentCombination < target) { |
| 87 | + l++; |
| 88 | + } else { |
| 89 | + r--; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + System.out.printf("Total pairs: %d", pairs); |
| 95 | + } |
| 96 | +} |
0 commit comments