-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem68.js
More file actions
26 lines (19 loc) · 808 Bytes
/
problem68.js
File metadata and controls
26 lines (19 loc) · 808 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
/*
Create a function that takes a sorted array of numbers and a target value as input. The function should find two numbers in the array that add up to the target value. Return an array containing the indices of the two numbers.
Example Input: ([1, 3, 6, 8, 11, 15], 9) Example Output: [1, 2] (numbers at indices 1 and 2: 3 + 6 = 9)
*/
const twoIndices = (arr, target) => {
const allNumbers = {};
for (let i = 0; i < arr.length; i++) {
const firstNum = arr[i];
const secondNum = target - firstNum;
if (allNumbers.hasOwnProperty(secondNum)) {
const firstIndex = allNumbers[secondNum];
return [firstIndex, i];
}
allNumbers[firstNum] = i;
}
return [];
}
// Example usage:
console.log(twoIndices([1, 3, 6, 8, 11, 15], 9));