Skip to content

Commit 479e925

Browse files
committed
--update: get pair of index for two numbers sum to N
1 parent 408dc13 commit 479e925

File tree

1 file changed

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

1 file changed

+14
-0
lines changed

src/_Problems_/find-2-nums-adding-to-n/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ function findTwoNumsAddingToN(arr, number) {
1616
return pair.length ? pair : false;
1717
}
1818

19+
function findIndicesOfTwoNumsAddingToN(nums, target) {
20+
const store = new Set();
21+
const pair = [];
22+
for (let index in nums) {
23+
if (store.has(target - nums[index])) {
24+
pair.push(nums.indexOf(target - nums[index]));
25+
pair.push(index);
26+
break;
27+
}
28+
store.add(nums[index]);
29+
}
30+
return pair.length ? pair : false;
31+
}
32+
1933
// the Brute force approach
2034
function findTwoNumsAddingToN2(arr, number) {
2135
const pair = [];

0 commit comments

Comments
 (0)