Skip to content

Commit 50115f9

Browse files
committed
--fix: change syntax, naming and comments
1 parent 2f8cd6f commit 50115f9

File tree

1 file changed

+16
-16
lines changed
  • src/_Problems_/find-2-nums-adding-to-n

1 file changed

+16
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
// the best case using a data structure - SET
2-
function findTwoNumsAddingToN(arr, sum) {
3-
const nums = [];
1+
// the best case [O(n)] using SET data structure
2+
function findTwoNumsAddingToN(arr, number) {
3+
const pair = [];
44
const store = new Set();
55

6-
for (let i = 0; i < arr.length; i++) {
7-
// check if the set contains one of the pir that sum upto given sum
8-
if (store.has(sum - arr[i])) {
9-
nums.push(sum - arr[i]);
10-
nums.push(arr[i]);
6+
for (let i = 0; i < arr.length; i += 1) {
7+
// check if the set contains one of the pair that sum upto given number
8+
if (store.has(number - arr[i])) {
9+
pair.push(number - arr[i]);
10+
pair.push(arr[i]);
1111
break;
1212
}
1313
// push the element in the set
1414
store.add(arr[i]);
1515
}
16-
return nums.length ? nums : false;
16+
return pair.length ? pair : false;
1717
}
1818

1919
// the Brute force approach
20-
function findTwoNumsAddingToN2(arr, sum) {
21-
const nums = [];
22-
for (let i = 0; i < arr.length; i++) {
23-
for (let j = i + 1; j < arr.length; j++) {
24-
if (arr[i] + arr[j] === sum) {
25-
nums.push(arr[i], arr[j]);
20+
function findTwoNumsAddingToN2(arr, number) {
21+
const pair = [];
22+
for (let i = 0; i < arr.length; i += 1) {
23+
for (let j = i + 1; j < arr.length; j += 1) {
24+
if (arr[i] + arr[j] === number) {
25+
pair.push(arr[i], arr[j]);
2626
break;
2727
}
2828
}
2929
}
3030

31-
return nums.length ? nums : false;
31+
return pair.length ? pair : null;
3232
}

0 commit comments

Comments
 (0)