|
1 | | -Explanation (C solution): |
| 1 | +## 🧮 Two Sum II - Input Array Is Sorted |
2 | 2 |
|
3 | | -## Two Sum II - Input Array is Sorted |
| 3 | +Difficulty: Easy |
| 4 | +Category: Arrays, Two Pointers |
| 5 | +Leetcode Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ |
4 | 6 |
|
5 | | -### Difficulty : Easy |
6 | | -### Category : Arrays, Two pointer |
| 7 | +### 📝 Introduction |
7 | 8 |
|
8 | | -Leetcode Link : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ |
| 9 | +You're given a sorted array of integers in non-decreasing order and a target value. The task is to find the indices of the two numbers such that they add up to the target value. The return should be in 1-based indexing, and you can assume exactly one solution exists and no element is reused. |
9 | 10 |
|
10 | | -### Introduction |
| 11 | +### 💡 Approach & Key Insights |
11 | 12 |
|
12 | | -Two Sum II, a slightly modified version of Two Sum I where we get a sorted array and a target. We need to find two numbers in the array such that they add up to the target and return their indices in 1-based indexing. |
| 13 | + Key Insight: Since the array is sorted, we can use the two-pointer technique instead of brute force. |
13 | 14 |
|
14 | | -### Intuition and Naive Approach/Brute Force |
| 15 | + Naive approach checks all pairs — this is simple but inefficient (O(n²)). |
15 | 16 |
|
16 | | -At first glance it seems simple. We could use two loops and check all the pairs and return the pair whose sum matches the target. |
| 17 | + Optimized approach uses two pointers (start and end) to shrink the search space intelligently in O(n) time. |
17 | 18 |
|
18 | | -for(int i=0; i < n; i++) { |
19 | | - for(int j=i+1; j < n; j++){ |
20 | | - if (num[i] + num[j] == target) |
21 | | - return [i+1, j+1] |
22 | | - } |
23 | | -} |
| 19 | + We return a dynamically allocated array in C to store the result as C functions can’t directly return multiple values. |
24 | 20 |
|
25 | | -While this would give us the result it would totally negate any advantage we have with the array being sorted and furthermore it would result in a time complexity of O(n^2) |
| 21 | +### 🛠️ Breakdown of Approaches |
26 | 22 |
|
27 | | -Space complexity: O(1) |
| 23 | +### 1️⃣ Brute Force / Naive Approach |
28 | 24 |
|
29 | | -### Optimized Solution |
30 | | -The solution's optimized with the utilization of two pointers - at the start and end of the array respectively, let's say l and r. |
31 | | -We move inwards into the array until our start and end pointers don't cross over i.e left > right. |
32 | | -We check if the value at array[l] + array[r] (which is our sum) == target. |
| 25 | +Explanation: |
| 26 | +Check every pair (i, j) where i < j. If numbers[i] + numbers[j] == target, return their 1-based indices. |
33 | 27 |
|
34 | | -Since it's a sorted array. If: |
35 | | -1. Sum is greater than Target |
36 | | --Then we know we need a smaller value to match or get close to the target, hence we decrease the end pointer , pointing to a smaller value (second largest value and so on..). |
37 | | -2. Sum is lesser than Target |
38 | | --Then we need a larger value to match or get close to the target, hence we increase the start pointer, pointing to a larger value(second smallest value and so on..). |
| 28 | +Time Complexity: O(n²) |
| 29 | +Each element is paired with every other element ahead of it. |
39 | 30 |
|
40 | | -If Sum is equal to our target: |
41 | | --Store the indexes of the two values in the malloced array (dynamic array since we can't directly return two values from a function in C) |
42 | | --We've increased the index by 1 to facilitate 1-based indexing as given in the problem. |
43 | | --Return the malloced array. |
| 31 | +Space Complexity: O(1) |
| 32 | +No extra space is used apart from result. |
| 33 | + |
| 34 | +Example / Dry Run: |
| 35 | + |
| 36 | +Example input: [2, 7, 11, 15], target = 9 |
| 37 | +Steps: |
| 38 | + |
| 39 | + 2+7=9 → match found |
| 40 | + Output: [1, 2] |
| 41 | + |
| 42 | +### 2️⃣ Optimized Approach |
| 43 | + |
| 44 | +Explanation: |
| 45 | +Use two pointers: |
| 46 | + |
| 47 | + Start l = 0 |
| 48 | + |
| 49 | + End r = n - 1 |
| 50 | + |
| 51 | +Repeat: |
| 52 | + |
| 53 | + If numbers[l] + numbers[r] == target → return [l+1, r+1] |
| 54 | + |
| 55 | + If sum is greater → move right pointer left |
| 56 | + |
| 57 | + If sum is less → move left pointer right |
44 | 58 |
|
45 | 59 | Time Complexity: O(n) |
46 | | -Space complexity: O(1) |
| 60 | +Only one pass through the array. |
| 61 | + |
| 62 | +Space Complexity: O(1) |
| 63 | +Only two pointers and result array of size 2. |
| 64 | + |
| 65 | +Example / Dry Run: |
| 66 | + |
| 67 | +Input: [2, 7, 11, 15], target = 9 |
| 68 | +Steps: |
| 69 | + |
| 70 | + l = 0, r = 3 → 2 + 15 = 17 → too big → r-- |
| 71 | + |
| 72 | + l = 0, r = 2 → 2 + 11 = 13 → still big → r-- |
| 73 | + |
| 74 | + l = 0, r = 1 → 2 + 7 = 9 → match! |
| 75 | + Output: [1, 2] |
| 76 | + |
| 77 | +### 3️⃣ Best / Final Optimized Approach (Same as above) |
| 78 | + |
| 79 | +Since the optimized two-pointer approach is already the most efficient for this problem, no further optimization is needed. |
| 80 | + |
| 81 | +### 📊 Complexity Analysis |
| 82 | + |
| 83 | +| Approach | Time Complexity | Space Complexity | |
| 84 | +| ------------- | --------------- | ---------------- | |
| 85 | +| Brute Force | O(n²) | O(1) | |
| 86 | +| Optimized | O(n) | O(1) | |
| 87 | +| Best Approach | O(n) | O(1) | |
| 88 | + |
| 89 | +### 📉 Optimization Ideas |
| 90 | + |
| 91 | + You can’t use hashing effectively here since the input is sorted, and space must be O(1). |
| 92 | + |
| 93 | + No need to explore binary search as two-pointer provides linear time with constant space. |
| 94 | + |
| 95 | +### 📌 Example Walkthroughs & Dry Runs |
| 96 | + |
| 97 | +Example 1: |
| 98 | + |
| 99 | +Input: [2, 7, 11, 15], target = 9 |
47 | 100 |
|
48 | | -### Example |
49 | | -Numbers = [2, 7, 11, 15], target = 9 |
| 101 | +Initial pointers: |
| 102 | +l = 0 → 2 |
| 103 | +r = 3 → 15 |
50 | 104 |
|
51 | | -The working : |
| 105 | +Step 1: 2 + 15 = 17 → too big → r = 2 |
| 106 | +Step 2: 2 + 11 = 13 → still big → r = 1 |
| 107 | +Step 3: 2 + 7 = 9 → match found → return [1, 2] |
52 | 108 |
|
53 | | -l = 0, r = 3 → 2 + 15 = 17 → sum larger than target, reduce right pointer |
| 109 | +### 🔗 Additional Resources |
54 | 110 |
|
55 | | -l = 0, r = 2 → 2 + 11 = 13 → sum larger than target, reduce right pointer |
| 111 | +- [Understanding Two Pointer Technique - GeeksForGeeks](https://www.geeksforgeeks.org/two-pointers-technique/) |
56 | 112 |
|
57 | | -l = 0, r = 1 → 2 + 7 = 9 → sum equal → return [1, 2] |
58 | 113 |
|
59 | | -Output: [1, 2] |
| 114 | +Author: hanzel-sc |
| 115 | +Date: 07/07/2025 |
0 commit comments