Skip to content

Commit 5dd7da3

Browse files
authored
Merge pull request #46 from hanzel-sc/two-sum-c
Solution - #167 - Hans - 07/04/2025
2 parents 93bceb5 + d7884d1 commit 5dd7da3

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
3+
int l = 0;
4+
int r = numbersSize-1;
5+
6+
int* answer = (int*)malloc(2*sizeof(int)); //dynamic memory allocation
7+
*returnSize = 2; //we're returning two values
8+
9+
while (l < r)
10+
{
11+
int sum = (numbers[l] + numbers[r]);
12+
if (sum == target)
13+
{
14+
answer[0] = l+1; //facilitating 1-based indexing as required by the problem.
15+
answer[1] = r+1;
16+
return answer;
17+
}
18+
else if (sum > target)
19+
{
20+
r--; //point to a smaller value to reduce the sum
21+
}
22+
else
23+
{
24+
l++; //point to a larger value to increase the sum
25+
}
26+
27+
}
28+
return 0;
29+
30+
}

0 commit comments

Comments
 (0)