Two Pointers Patterns
O(n)O(1)
1
3
4
5
7
10
12
15
18
20
Code:
function twoSumSorted(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
if (arr[left] + arr[right] === target) {
return [left, right];
}
if (arr[left] + arr[right] < target) {
left++;
} else {
right--;
}
}
return [];
};