Two Sum II - Input Array Is Sorted - Complete Solution Guide
Two Sum II - Input Array Is Sorted is LeetCode problem 167, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a 1-indexed 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. Let these two numbers be numbers[index 1 ] and numbers[index 2 ] where 1 <= index 1 < index 2 <= numbers.length . Return the indices of the two numbers, index 1 and index 2 , added by one as an integer array [index 1 , index 2 ] of length 2. The tests are generated such that there is exactly one solution . You may not use the same elemen
Detailed Explanation
The problem 'Two Sum II - Input Array Is Sorted' requires us to find two numbers within a sorted array that add up to a given target value. We must return the 1-based indices of these two numbers. The array is guaranteed to have exactly one solution, and we cannot use the same element twice. Crucially, we are given that the input array `numbers` is already sorted in non-decreasing order. The problem emphasizes the need for a solution with constant extra space.
Solution Approach
The provided solution employs the Two Pointers technique. We initialize two pointers, `left` and `right`, to the beginning and end of the array, respectively. We then iterate through the array, adjusting the pointers based on the sum of the elements they point to. If the sum is less than the target, we move the `left` pointer to the right, increasing the sum. If the sum is greater than the target, we move the `right` pointer to the left, decreasing the sum. When the sum equals the target, we have found the two numbers, and we return their 1-based indices.
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `left` to 0 and `right` to `len(numbers) - 1`.
- Step 2: While `left` is less than `right`, calculate the sum of `numbers[left]` and `numbers[right]`.
- Step 3: If the `current_sum` is equal to the `target`, return `[left + 1, right + 1]` (adding 1 to convert to 1-based indices).
- Step 4: If the `current_sum` is less than the `target`, increment `left` by 1 to increase the sum.
- Step 5: If the `current_sum` is greater than the `target`, decrement `right` by 1 to decrease the sum.
- Step 6: If no solution is found within the while loop (which shouldn't happen given the problem constraints), return an empty array or null (depending on the language).
Key Insights
- Insight 1: The array being sorted allows us to use the Two Pointers technique or Binary Search for an efficient solution.
- Insight 2: The Two Pointers approach is particularly well-suited for this problem due to its O(1) space complexity.
- Insight 3: Since we need to return 1-based indices, we have to increment the 0-based indices obtained from the algorithm.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers, Binary Search.
Companies
Asked at: EPAM Systems, Infosys, J.P. Morgan, Oracle, Qualcomm, Yandex, Zomato, eBay.