This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathArrayPairSum.java
More file actions
119 lines (111 loc) · 3.52 KB
/
Copy pathArrayPairSum.java
File metadata and controls
119 lines (111 loc) · 3.52 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.ArrayList;
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<int[]> getAllSumPairs(int k, int[] input) {
ArrayList<int[]> allSumPairs = new ArrayList<int[]>();
HashMap<Integer, Boolean> usedNumbers = new HashMap<Integer, Boolean>();
for (int i = 0; i < input.length; ++i) {
int difference = k - input[i];
if (usedNumbers.containsKey(difference)
&& !usedNumbers.get(difference)) {
int[] sumPair = new int[2];
sumPair[0] = input[i];
sumPair[1] = difference;
allSumPairs.add(sumPair);
usedNumbers.put(input[i], true);
} else {
usedNumbers.put(input[i], false);
}
}
return allSumPairs;
}
public static void printAllPairSums(ArrayList<int[]> allPairSums) {
String output = "[ ";
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<int[]> getAllSumPairsWithRep(int sum, int[] input) {
ArrayList<int[]> allSumPairs = new ArrayList<int[]>();
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<int[]> getAllSumPairsWithRepBetter(int sum, int[] input) {
ArrayList<int[]> allSumPairs = new ArrayList<int[]>();
HashMap<Integer, Integer> checker = new HashMap<Integer, Integer>();
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;
}
}