diff --git a/solutions/java/ArrayPairSum.java b/solutions/java/ArrayPairSum.java index eeccfed..42432fa 100644 --- a/solutions/java/ArrayPairSum.java +++ b/solutions/java/ArrayPairSum.java @@ -2,15 +2,25 @@ import java.util.Arrays; import java.util.HashMap; - public class ArrayPairSum { - + + /** + * Does not consider duplicates of numbers + * + * @param Sum + * . Example: 8 + * @param Integer + * array. Example: [3, 4, 5, 4, 4] + * @return List of Integer pairs. Example: [[3, 5], [4, 4]]. Does not + * consider multiple occurrences of number 4. + */ public ArrayList getAllSumPairs(int k, int[] input) { ArrayList allSumPairs = new ArrayList(); HashMap usedNumbers = new HashMap(); for (int i = 0; i < input.length; ++i) { int difference = k - input[i]; - if (usedNumbers.containsKey(difference) && !usedNumbers.get(difference)) { + if (usedNumbers.containsKey(difference) + && !usedNumbers.get(difference)) { int[] sumPair = new int[2]; sumPair[0] = input[i]; sumPair[1] = difference; @@ -22,22 +32,88 @@ public ArrayList getAllSumPairs(int k, int[] input) { } return allSumPairs; } - + public static void printAllPairSums(ArrayList allPairSums) { String output = "[ "; - for (int[] pairSum: allPairSums) { + for (int[] pairSum : allPairSums) { output += Arrays.toString(pairSum); output += " "; } output += " ]"; System.out.println(output); - } - + } + public static void main(String[] arg) { ArrayPairSum a = new ArrayPairSum(); int[] input1 = { 3, 4, 5, 6, 7 }; printAllPairSums(a.getAllSumPairs(10, input1)); int[] input2 = { 3, 4, 5, 4, 4 }; printAllPairSums(a.getAllSumPairs(8, input2)); + + printAllPairSums(a.getAllSumPairsWithRep(10, input1)); + printAllPairSums(a.getAllSumPairsWithRep(8, input2)); + + printAllPairSums(a.getAllSumPairsWithRepBetter(10, input1)); + printAllPairSums(a.getAllSumPairsWithRepBetter(8, input2)); + } + + /** + * Considers duplicates of numbers. + * + * @param Sum + * . Example: 8 + * @param Integer + * array. Example: [3, 4, 5, 4, 4] + * @return List of Integer pairs. Example: [[3, 5], [4, 4], [4, 4], [4, 4]]. + * Considers multiple occurrences of number 4. + * @complexity O(n2) best/worst case. + */ + public ArrayList getAllSumPairsWithRep(int sum, int[] input) { + ArrayList allSumPairs = new ArrayList(); + for (int i = 0; i < input.length - 1; i++) { + for (int j = i + 1; j < input.length; j++) { + int tSum = input[i] + input[j]; + if (sum == tSum) { + int[] sumPair = new int[2]; + sumPair[0] = input[i]; + sumPair[1] = input[j]; + allSumPairs.add(sumPair); + } + } + } + return allSumPairs; + } + + /** + * Considers duplicates of numbers. + * + * @param Sum + * . Example: 8 + * @param Integer + * array. Example: [3, 4, 5, 4, 4] + * @return List of Integer pairs. Example: [[3, 5], [4, 4], [4, 4], [4, 4]]. + * Considers multiple occurrences of number 4. + * @complexity O(n) best case - when all numbers are unique. O(n2) worst + * case - when all numbers are the same. + */ + public ArrayList getAllSumPairsWithRepBetter(int sum, int[] input) { + ArrayList allSumPairs = new ArrayList(); + HashMap checker = new HashMap(); + for (int i = 0; i < input.length; i++) { + int diff = sum - input[i]; + if (checker.containsKey(diff) && checker.get(diff) != null + && checker.get(diff) > 0) { + for (int j = 0; j < checker.get(diff); j++) { + int[] sumPair = new int[2]; + sumPair[0] = input[i]; + sumPair[1] = diff; + allSumPairs.add(sumPair); + } + checker.put(input[i], checker.get(diff) + 1); + } else { + checker.put(input[i], 1); + } + } + return allSumPairs; } }