-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSumSorted.js
More file actions
executable file
·44 lines (29 loc) · 1.1 KB
/
twoSumSorted.js
File metadata and controls
executable file
·44 lines (29 loc) · 1.1 KB
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
36
37
38
39
40
41
42
43
44
// two pointers
// function to calculate two sum in a sorted array
// what is two sum ?
// two sum is a problem where we need to find two numbers in a sorted array that add up to a specific target sum
// example :
// input: nums = [2,3,4,5,6,7,8,9], target = 10
// output: [0,6]
// explanation: because nums[0] + nums[6] == 10, we return [0, 6]
function twoSumSorted(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let sum = nums[left] + nums[right];
if (sum === target) {
return [left, right];
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
// test the function
console.log(twoSumSorted([2, 3, 4, 5, 6, 7, 8, 9], 10)); // Output: [0, 6]
console.log(twoSumSorted([1, 2, 3, 4, 6, 8, 9], 12)); // Output: [2, 6]
console.log(twoSumSorted([0, 1, 2, 3, 4, 5, 6], 7)); // Output: [1, 6]
console.log(twoSumSorted([-3, -1, 0, 2, 4, 5], 1)); // Output: [2, 4]
console.log(twoSumSorted([1, 2, 3, 4, 5, 6, 7, 8, 9], 17)); // Output: [7, 8]
console.log(twoSumSorted([-5, -4, -3, -2, -1, 0, 1, 2], -3)); // Output: [0, 4]