167. Two Sum II - Input Array Is Sorted

 

Solving the Two Sum II Problem with a Two-Pointer Technique in JavaScript

When faced with the task of finding two numbers in a sorted array that sum up to a specific target, the Two Sum II problem on LeetCode provides a classic challenge. In this post, we'll explore an efficient and elegant solution using the two-pointer technique in JavaScript. This approach ensures that we find the solution in linear time.

Problem Statement

Given an array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Return the indices of the two numbers (1-based index) as an integer array [index1, index2]. You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example

Input: numbers = [2, 7, 11, 15], target = 9 Output: [1, 2]

Intuition

Since the array is sorted, we can efficiently find the two numbers using the two-pointer technique. This method involves initializing two pointers at different ends of the array and moving them inward based on the sum of the elements at these pointers.

Approach

  1. Initialize Pointers:

    • Start with one pointer (si) at the beginning of the array.

    • Place the other pointer (li) at the end of the array.

  2. Iterate with a While Loop:

    • Use a while loop to move the pointers until they meet.

    • If the sum of the numbers at the two pointers equals the target, return their 1-based indices.

    • If the sum is greater than the target, decrement the end pointer to reduce the sum.

    • If the sum is less than the target, increment the start pointer to increase the sum.

Code Implementation

Here's the JavaScript implementation of the Two Sum II problem using the two-pointer technique:

javascript
var twoSum = function(numbers, target) {
    let si = 0;
    let li = numbers.length - 1;
    while (si <= li) {
        if (numbers[si] + numbers[li] === target) {
            return [si + 1, li + 1];
        } else if (numbers[si] + numbers[li] > target) {
            li--;
        } else {
            si++;
        }
    }
};

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the numbers array. This is because each element is checked at most once due to the two-pointer technique.

  • Space Complexity: O(1), since we only use a few extra variables regardless of the input size.

Conclusion

The two-pointer technique is a powerful tool when dealing with sorted arrays. It allows us to efficiently find pairs of elements that meet certain criteria, such as summing to a specific target. By leveraging this approach, we can solve the Two Sum II problem in linear time, making it both elegant and efficient.

For more detailed explanations and additional solutions, check out the discussion on LeetCode: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/solutions/6146809/easy-and-simple-approach-by-bikash_kumar-88wv/ .

Happy coding! 😊

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix