diff --git a/coding_interviews/leetcode/medium/two-sum-ii-input-array-is-sorted/two-sum-ii-input-array-is-sorted-2.js b/coding_interviews/leetcode/medium/two-sum-ii-input-array-is-sorted/two-sum-ii-input-array-is-sorted-2.js new file mode 100644 index 00000000..126e5cb0 --- /dev/null +++ b/coding_interviews/leetcode/medium/two-sum-ii-input-array-is-sorted/two-sum-ii-input-array-is-sorted-2.js @@ -0,0 +1,20 @@ +function twoSum(numbers, target) { + let start = 0; + let end = numbers.length - 1; + let result = []; + + while (start < end) { + let sum = numbers[start] + numbers[end]; + + if (sum === target) { + result.push(start + 1); + result.push(end + 1); + break; + } + + if (sum > target) end--; + else start++; + } + + return result; +}