forked from msvz/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_sum_pairs.js
More file actions
30 lines (28 loc) · 865 Bytes
/
Copy pathfind_sum_pairs.js
File metadata and controls
30 lines (28 loc) · 865 Bytes
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
/*
**************************************************************************************************************
Given an array A[] and a number x, check for pair in A[] with sum as x
soln 1: n square solution. Loop
soln 2: Sort using quicksort. (n log n + n log n)
a + b = k
for every a, find k - a.
soln 3: store key, count
loop through array -> find k - arr
**************************************************************************************************************
*/
function findPairs (arr, k) {
hash = {};
arr.forEach(function(item) {
if (hash.hasOwnProperty(item)) {
hash[item] = hash[item] + 1;
} else {
hash[item] = 0;
}
});
arr.forEach(function(a) {
b = k - a;
if(hash.hasOwnProperty(b)) {
console.log(a + "+" + b + "=" + k);
}
});
}
findPairs([ 1, 2, 2, 3, 9, 6, 29, 99, 66, 33, 22,77, 62, 37 ], 99);