2
2
import java .util .Arrays ;
3
3
import java .util .HashMap ;
4
4
5
-
6
5
public class ArrayPairSum {
7
-
6
+
7
+ /**
8
+ * Does not consider duplicates of numbers
9
+ *
10
+ * @param Sum
11
+ * . Example: 8
12
+ * @param Integer
13
+ * array. Example: [3, 4, 5, 4, 4]
14
+ * @return List of Integer pairs. Example: [[3, 5], [4, 4]]. Does not
15
+ * consider multiple occurrences of number 4.
16
+ */
8
17
public ArrayList <int []> getAllSumPairs (int k , int [] input ) {
9
18
ArrayList <int []> allSumPairs = new ArrayList <int []>();
10
19
HashMap <Integer , Boolean > usedNumbers = new HashMap <Integer , Boolean >();
11
20
for (int i = 0 ; i < input .length ; ++i ) {
12
21
int difference = k - input [i ];
13
- if (usedNumbers .containsKey (difference ) && !usedNumbers .get (difference )) {
22
+ if (usedNumbers .containsKey (difference )
23
+ && !usedNumbers .get (difference )) {
14
24
int [] sumPair = new int [2 ];
15
25
sumPair [0 ] = input [i ];
16
26
sumPair [1 ] = difference ;
@@ -22,22 +32,88 @@ public ArrayList<int[]> getAllSumPairs(int k, int[] input) {
22
32
}
23
33
return allSumPairs ;
24
34
}
25
-
35
+
26
36
public static void printAllPairSums (ArrayList <int []> allPairSums ) {
27
37
String output = "[ " ;
28
- for (int [] pairSum : allPairSums ) {
38
+ for (int [] pairSum : allPairSums ) {
29
39
output += Arrays .toString (pairSum );
30
40
output += " " ;
31
41
}
32
42
output += " ]" ;
33
43
System .out .println (output );
34
- }
35
-
44
+ }
45
+
36
46
public static void main (String [] arg ) {
37
47
ArrayPairSum a = new ArrayPairSum ();
38
48
int [] input1 = { 3 , 4 , 5 , 6 , 7 };
39
49
printAllPairSums (a .getAllSumPairs (10 , input1 ));
40
50
int [] input2 = { 3 , 4 , 5 , 4 , 4 };
41
51
printAllPairSums (a .getAllSumPairs (8 , input2 ));
52
+
53
+ printAllPairSums (a .getAllSumPairsWithRep (10 , input1 ));
54
+ printAllPairSums (a .getAllSumPairsWithRep (8 , input2 ));
55
+
56
+ printAllPairSums (a .getAllSumPairsWithRepBetter (10 , input1 ));
57
+ printAllPairSums (a .getAllSumPairsWithRepBetter (8 , input2 ));
58
+ }
59
+
60
+ /**
61
+ * Considers duplicates of numbers.
62
+ *
63
+ * @param Sum
64
+ * . Example: 8
65
+ * @param Integer
66
+ * array. Example: [3, 4, 5, 4, 4]
67
+ * @return List of Integer pairs. Example: [[3, 5], [4, 4], [4, 4], [4, 4]].
68
+ * Considers multiple occurrences of number 4.
69
+ * @complexity O(n2) best/worst case.
70
+ */
71
+ public ArrayList <int []> getAllSumPairsWithRep (int sum , int [] input ) {
72
+ ArrayList <int []> allSumPairs = new ArrayList <int []>();
73
+ for (int i = 0 ; i < input .length - 1 ; i ++) {
74
+ for (int j = i + 1 ; j < input .length ; j ++) {
75
+ int tSum = input [i ] + input [j ];
76
+ if (sum == tSum ) {
77
+ int [] sumPair = new int [2 ];
78
+ sumPair [0 ] = input [i ];
79
+ sumPair [1 ] = input [j ];
80
+ allSumPairs .add (sumPair );
81
+ }
82
+ }
83
+ }
84
+ return allSumPairs ;
85
+ }
86
+
87
+ /**
88
+ * Considers duplicates of numbers.
89
+ *
90
+ * @param Sum
91
+ * . Example: 8
92
+ * @param Integer
93
+ * array. Example: [3, 4, 5, 4, 4]
94
+ * @return List of Integer pairs. Example: [[3, 5], [4, 4], [4, 4], [4, 4]].
95
+ * Considers multiple occurrences of number 4.
96
+ * @complexity O(n) best case - when all numbers are unique. O(n2) worst
97
+ * case - when all numbers are the same.
98
+ */
99
+ public ArrayList <int []> getAllSumPairsWithRepBetter (int sum , int [] input ) {
100
+ ArrayList <int []> allSumPairs = new ArrayList <int []>();
101
+ HashMap <Integer , Integer > checker = new HashMap <Integer , Integer >();
102
+ for (int i = 0 ; i < input .length ; i ++) {
103
+ int diff = sum - input [i ];
104
+ if (checker .containsKey (diff ) && checker .get (diff ) != null
105
+ && checker .get (diff ) > 0 ) {
106
+ for (int j = 0 ; j < checker .get (diff ); j ++) {
107
+ int [] sumPair = new int [2 ];
108
+ sumPair [0 ] = input [i ];
109
+ sumPair [1 ] = diff ;
110
+ allSumPairs .add (sumPair );
111
+ }
112
+ checker .put (input [i ], checker .get (diff ) + 1 );
113
+ } else {
114
+ checker .put (input [i ], 1 );
115
+ }
116
+ }
117
+ return allSumPairs ;
42
118
}
43
119
}
0 commit comments