forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistinctPairs.java
50 lines (44 loc) · 1.58 KB
/
DistinctPairs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.rampatra.arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Level: Easy
* Problem Description:
* Given an array and a target sum, return the number of distinct pairs whose sum is equal to the target sum.
* <p>
* For Example, given an array [1, 2, 3, 6, 7, 8, 9, 1] and a target sum of 10,
* the 7 pairs, i.e, (1, 9), (2, 8), (3, 7), (8, 2), (9, 1), (9, 1), and (1, 9) all sum to 10 but there are only
* three distinct pairs, i.e, (1, 9), (2, 8), and (3, 7) so the answer would be 3.
*
* @author rampatra
* @since 2019-06-03
*/
public class DistinctPairs {
/**
* Time complexity: O(n), n = size of the array
* Space complexity: O(n)
*
* @param arr
* @param targetSum
* @return
*/
private static int numberOfDistinctPairs(int[] arr, int targetSum) {
Set<Integer> numSet = new HashSet<>();
Set<Set<Integer>> pairSet = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
if (numSet.contains(targetSum - arr[i])) {
Set<Integer> pair = new HashSet<>();
pair.add(arr[i]);
pair.add(targetSum - arr[i]);
pairSet.add(pair);
}
numSet.add(arr[i]);
}
return pairSet.size();
}
public static void main(String[] args) {
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 1));
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 2));
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 10));
}
}