Skip to content

Commit 95487ab

Browse files
committed
Improve twosum2 algorithm
1 parent e50b2af commit 95487ab

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/javascript/algorithms/array/2.twoSum2/twoSum2.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ function findTwoSumIndices(nums, target){
33
let left = 0, right = nums.length-1;
44

55
while(left < right) {
6-
let total = nums[left] + nums[right];
6+
const total = nums[left] + nums[right];
77

88
if(total === target) {
9-
return [left+1, right+1];
9+
return [left+1, right+1]; // Return 1-based indices
1010
} else if(total > target) {
11-
right--;
11+
right--; //Move the right pointer leftward
1212
} else {
13-
left++;
13+
left++; //Move the left pointer rightward
1414
}
1515
}
1616

@@ -21,5 +21,6 @@ let nums1 = [2,7,11,15,9];
2121
let target1 = 16;
2222
let nums2 = [-2, 0, 3, 5];
2323
let target2 = 1;
24-
console.log(findTwoSumIndices(nums1, target1));
25-
console.log(findTwoSumIndices(nums2, target2));
24+
// Output results
25+
console.log(`Indices for target ${target1} in nums1:`, findTwoSumIndices(nums1, target1));
26+
console.log(`Indices for target ${target2} in nums2:`, findTwoSumIndices(nums2, target2));
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
1-
**Description:**
1+
# Two Sum II - Algorithm Explanation
2+
3+
## **Problem Description**
24
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.
35

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`.
57

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+
---
109

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+
```
1315

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:
1518

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`).
1722

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.
1925

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`.
2133

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`.
2336

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]`.
2539

26-
7. Return `[-1, -1]` incase of no matching numbers whose sum not equal to the target.
40+
---
2741

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.
3045

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

Comments
 (0)