Skip to content

Commit fc8a204

Browse files
committed
Merge pull request blakeembrey#120 from roitt/array-pair-sum-change
Solutions considering duplicates in the array.
2 parents 07e70f6 + 3ebf0b4 commit fc8a204

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

solutions/java/ArrayPairSum.java

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
import java.util.Arrays;
33
import java.util.HashMap;
44

5-
65
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+
*/
817
public ArrayList<int[]> getAllSumPairs(int k, int[] input) {
918
ArrayList<int[]> allSumPairs = new ArrayList<int[]>();
1019
HashMap<Integer, Boolean> usedNumbers = new HashMap<Integer, Boolean>();
1120
for (int i = 0; i < input.length; ++i) {
1221
int difference = k - input[i];
13-
if (usedNumbers.containsKey(difference) && !usedNumbers.get(difference)) {
22+
if (usedNumbers.containsKey(difference)
23+
&& !usedNumbers.get(difference)) {
1424
int[] sumPair = new int[2];
1525
sumPair[0] = input[i];
1626
sumPair[1] = difference;
@@ -22,22 +32,88 @@ public ArrayList<int[]> getAllSumPairs(int k, int[] input) {
2232
}
2333
return allSumPairs;
2434
}
25-
35+
2636
public static void printAllPairSums(ArrayList<int[]> allPairSums) {
2737
String output = "[ ";
28-
for (int[] pairSum: allPairSums) {
38+
for (int[] pairSum : allPairSums) {
2939
output += Arrays.toString(pairSum);
3040
output += " ";
3141
}
3242
output += " ]";
3343
System.out.println(output);
34-
}
35-
44+
}
45+
3646
public static void main(String[] arg) {
3747
ArrayPairSum a = new ArrayPairSum();
3848
int[] input1 = { 3, 4, 5, 6, 7 };
3949
printAllPairSums(a.getAllSumPairs(10, input1));
4050
int[] input2 = { 3, 4, 5, 4, 4 };
4151
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;
42118
}
43119
}

0 commit comments

Comments
 (0)