Skip to content

Commit d3144db

Browse files
committed
Correct array pair sum
1 parent e144126 commit d3144db

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

problems/array-pair-sum/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Array Pair Sum
22

3-
Given an integer array, output all pairs that sum up to a specific value k. Consider the fact that the same number can add up to `k` with its duplicates in the array.
3+
Given an integer array, output all distinct pairs that sum up to a specific value k. Consider the fact that the same number can add up to `k` with its duplicates in the array.
44

55
> For example the array is [1, 1, 2, 3, 4] and the desired sum is 4. Should we output the pair (1, 3) twice or just once? Also do we output the reverse of a pair, meaning both (3, 1) and (1, 3)? Let’s keep the output as short as possible and print each pair only once. So, we will output only one copy of (1, 3). Also note that we shouldn’t output (2, 2) because it’s not a pair of two distinct elements.
66
77
## Example
88

99
```
1010
f(10, [3, 4, 5, 6, 7]) // [ [6, 4], [7, 3] ]
11-
f(8, [3, 4, 5, 4, 4]) // [ [3, 5], [4, 4], [4, 4], [4, 4] ]
11+
f(8, [3, 4, 5, 4, 4]) // [ [3, 5], [4, 4] ]
1212
```
1313

1414
## Source
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
module.exports = function (sum, array) {
2-
var results = [];
3-
4-
for (var i = 0, len = array.length; i < len; i++) {
5-
for (var j = i + 1; j < len; j++) {
6-
if (array[i] + array[j] === sum) {
7-
results.push([array[i], array[j]]);
8-
}
1+
module.exports = function (k, array) {
2+
var hash = {};
3+
var pairs = [];
4+
5+
// Loop through the array once, storing the results in an object for a
6+
// time complexity of O(n) - the naive solution consists of two for loops
7+
// which results in a complexity of O(n^2)
8+
array.forEach(function (number) {
9+
var target = k - number;
10+
11+
// Make sure the value in unused and it's a unique pair
12+
if (hash[target] === 1 && number + target === k) {
13+
pairs.push([target, number]);
14+
} else {
15+
hash[number] = 0;
916
}
10-
}
1117

12-
return results;
18+
hash[number]++;
19+
});
20+
21+
return pairs;
1322
};

tests/javascript/array-pair-sum.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ var arrayPairSum = require('../../solutions/javascript/array-pair-sum');
33

44
describe('Array Pair Sum', function () {
55
it('should find pairs that equal the expected sum', function () {
6-
expect(arrayPairSum(10, [3, 4, 5, 6, 7])).to.eql([[3, 7], [4, 6]]);
6+
expect(arrayPairSum(10, [3, 4, 5, 6, 7])).to.eql([[4, 6], [3, 7]]);
77
});
88

9-
it('should allow for duplicate results', function () {
10-
expect(arrayPairSum(8, [3, 4, 5, 4, 4])).to.eql([[3, 5], [4, 4], [4, 4], [4, 4]]);
9+
it('should not output duplicate results', function () {
10+
expect(arrayPairSum(8, [3, 4, 5, 4, 4])).to.eql([[3, 5], [4, 4]]);
11+
});
12+
13+
it('should only allow distinct elements', function () {
14+
expect(arrayPairSum(4, [1, 1, 2, 3, 4])).to.eql([[1, 3]]);
1115
});
1216
});

0 commit comments

Comments
 (0)