|
1 |
| -**Description:** |
| 1 | +# Two Sum II - Algorithm Explanation |
| 2 | + |
| 3 | +## **Problem Description** |
2 | 4 | Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
|
3 | 5 |
|
4 |
| -The function twoSum should return indices of the two numbers such that they add up to the target, where `index1` must be less than `index2`. |
| 6 | +The function `twoSum` should return the indices of the two numbers such that they add up to the target, where `index1` must be less than `index2`. |
5 | 7 |
|
6 |
| -## Example: |
7 |
| -Input: numbers = [2,7,11,15,9], target = 16 |
8 |
| -Output: [2,5] |
9 |
| -Explanation: The sum of 7 and 9 is 16. Therefore index1 = 2, index2 = 5. |
| 8 | +--- |
10 | 9 |
|
11 |
| -**Algorithmic Steps:** |
12 |
| -This problem is solved with the help of **two pointers** technique. The algorithmic approach can be summarized as follows: |
| 10 | +## **Example** |
| 11 | +### Input: |
| 12 | +```plaintext |
| 13 | +numbers = [2, 7, 11, 15, 9], target = 16 |
| 14 | +``` |
13 | 15 |
|
14 |
| -1. Initialize `left` and `right` pointers to `0` and last index(`length-1`) of the input array. |
| 16 | +## **Algorithmic Steps** |
| 17 | +The problem is solved using the **two pointers** technique. The algorithm can be summarized as follows: |
15 | 18 |
|
16 |
| -2. Iterate over input array and find the `total` of left and right elements each time. |
| 19 | +1. **Initialize Pointers:** |
| 20 | + - Set the `left` pointer to the start of the array (index `0`). |
| 21 | + - Set the `right` pointer to the last index of the array (`length - 1`). |
17 | 22 |
|
18 |
| -3. If the total is equal to the target, return the left and right indexes incremented by one. By default, left and right variables indexed with zero where the output needs to be 1 index based. |
| 23 | +2. **Iterate and Calculate:** |
| 24 | + - Compute the `total` by summing the elements at the `left` and `right` pointers. |
19 | 25 |
|
20 |
| -4. If the total is greater than target, decrement the right pointer to decrease the total value as per given target. |
| 26 | +3. **Check Conditions:** |
| 27 | + - If `total == target`: |
| 28 | + - Return the indices `[left + 1, right + 1]` (convert 0-based indices to 1-based). |
| 29 | + - If `total > target`: |
| 30 | + - Decrement the `right` pointer to reduce the `total`. |
| 31 | + - If `total < target`: |
| 32 | + - Increment the `left` pointer to increase the `total`. |
21 | 33 |
|
22 |
| -5. If the total is less than target, increment the left pointer to increase the total value as per given target. |
| 34 | +4. **Repeat:** |
| 35 | + - Continue steps 2 and 3 until the two numbers sum up to the `target`. |
23 | 36 |
|
24 |
| -6. Repeat 2-5 steps until the two numbers sum is equal to the target. |
| 37 | +5. **Handle Edge Cases:** |
| 38 | + - If no such numbers exist that sum to the `target`, return `[-1, -1]`. |
25 | 39 |
|
26 |
| -7. Return `[-1, -1]` incase of no matching numbers whose sum not equal to the target. |
| 40 | +--- |
27 | 41 |
|
28 |
| -**Time and Space complexity:** |
29 |
| -This algorithm takes a time complexity of O(n), where n is the number of elements in an array. This is because we are traversing the array at most once. |
| 42 | +## **Time and Space Complexity** |
| 43 | +- **Time Complexity:** |
| 44 | + - The algorithm runs in **O(n)**, where `n` is the number of elements in the array. This is because we traverse the array at most once. |
30 | 45 |
|
31 |
| -Here, we don't use any additional datastructure other than two constant pointer variables. Hence, the space complexity will be O(1). |
| 46 | +- **Space Complexity:** |
| 47 | + - The space complexity is **O(1)**, as only two pointer variables are used, and no additional data structures are required. |
0 commit comments