|
| 1 | +## 🧮 Two Sum II - Input Array Is Sorted |
| 2 | + |
| 3 | +Difficulty: Easy |
| 4 | +Category: Arrays, Two Pointers |
| 5 | +Leetcode Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ |
| 6 | + |
| 7 | +### 📝 Introduction |
| 8 | + |
| 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. |
| 10 | + |
| 11 | +### 💡 Approach & Key Insights |
| 12 | + |
| 13 | + Key Insight: Since the array is sorted, we can use the two-pointer technique instead of brute force. |
| 14 | + |
| 15 | + Naive approach checks all pairs — this is simple but inefficient (O(n²)). |
| 16 | + |
| 17 | + Optimized approach uses two pointers (start and end) to shrink the search space intelligently in O(n) time. |
| 18 | + |
| 19 | + We return a dynamically allocated array in C to store the result as C functions can’t directly return multiple values. |
| 20 | + |
| 21 | +### 🛠️ Breakdown of Approaches |
| 22 | + |
| 23 | +### 1️⃣ Brute Force / Naive Approach |
| 24 | + |
| 25 | +Explanation: |
| 26 | +Check every pair (i, j) where i < j. If numbers[i] + numbers[j] == target, return their 1-based indices. |
| 27 | + |
| 28 | +Time Complexity: O(n²) |
| 29 | +Each element is paired with every other element ahead of it. |
| 30 | + |
| 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 |
| 58 | + |
| 59 | +Time Complexity: O(n) |
| 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 |
| 100 | + |
| 101 | +Initial pointers: |
| 102 | +l = 0 → 2 |
| 103 | +r = 3 → 15 |
| 104 | + |
| 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] |
| 108 | + |
| 109 | +### 🔗 Additional Resources |
| 110 | + |
| 111 | +- [Understanding Two Pointer Technique - GeeksForGeeks](https://www.geeksforgeeks.org/two-pointers-technique/) |
| 112 | + |
| 113 | + |
| 114 | +Author: hanzel-sc |
| 115 | +Date: 07/07/2025 |
0 commit comments