forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (31 loc) · 836 Bytes
/
index.js
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
// the best case [O(n)] using SET data structure
function findTwoNumsAddingToN(arr, number) {
const pair = [];
const store = new Set();
for (let i = 0; i < arr.length; i += 1) {
// check if the set contains one of the element that sum upto the given number
if (store.has(number - arr[i])) {
pair.push(number - arr[i]);
pair.push(arr[i]);
break;
}
// push the element in the set
store.add(arr[i]);
}
return pair.length ? pair : false;
}
// the Brute force approach
function findTwoNumsAddingToN2(arr, number) {
for (let i = 0; i < arr.length; i += 1) {
for (let j = i + 1; j < arr.length; j += 1) {
if (arr[i] + arr[j] === number) {
return [arr[i], arr[j]];
}
}
}
return false;
}
module.exports = {
findTwoNumsAddingToN,
findTwoNumsAddingToN2,
};