diff --git a/Arrays & Strings/Two Sum/Explanation.md b/Arrays & Strings/Two Sum/Explanation.md new file mode 100644 index 0000000..786375a --- /dev/null +++ b/Arrays & Strings/Two Sum/Explanation.md @@ -0,0 +1,115 @@ +## ๐Ÿงฎ Two Sum II - Input Array Is Sorted + +Difficulty: Easy +Category: Arrays, Two Pointers +Leetcode Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ + +### ๐Ÿ“ Introduction + +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. + +### ๐Ÿ’ก Approach & Key Insights + + Key Insight: Since the array is sorted, we can use the two-pointer technique instead of brute force. + + Naive approach checks all pairs โ€” this is simple but inefficient (O(nยฒ)). + + Optimized approach uses two pointers (start and end) to shrink the search space intelligently in O(n) time. + + We return a dynamically allocated array in C to store the result as C functions canโ€™t directly return multiple values. + +### ๐Ÿ› ๏ธ Breakdown of Approaches + +### 1๏ธโƒฃ Brute Force / Naive Approach + +Explanation: +Check every pair (i, j) where i < j. If numbers[i] + numbers[j] == target, return their 1-based indices. + +Time Complexity: O(nยฒ) +Each element is paired with every other element ahead of it. + +Space Complexity: O(1) +No extra space is used apart from result. + +Example / Dry Run: + +Example input: [2, 7, 11, 15], target = 9 +Steps: + + 2+7=9 โ†’ match found + Output: [1, 2] + +### 2๏ธโƒฃ Optimized Approach + +Explanation: +Use two pointers: + + Start l = 0 + + End r = n - 1 + +Repeat: + + If numbers[l] + numbers[r] == target โ†’ return [l+1, r+1] + + If sum is greater โ†’ move right pointer left + + If sum is less โ†’ move left pointer right + +Time Complexity: O(n) +Only one pass through the array. + +Space Complexity: O(1) +Only two pointers and result array of size 2. + +Example / Dry Run: + +Input: [2, 7, 11, 15], target = 9 +Steps: + + l = 0, r = 3 โ†’ 2 + 15 = 17 โ†’ too big โ†’ r-- + + l = 0, r = 2 โ†’ 2 + 11 = 13 โ†’ still big โ†’ r-- + + l = 0, r = 1 โ†’ 2 + 7 = 9 โ†’ match! + Output: [1, 2] + +### 3๏ธโƒฃ Best / Final Optimized Approach (Same as above) + +Since the optimized two-pointer approach is already the most efficient for this problem, no further optimization is needed. + +### ๐Ÿ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | --------------- | ---------------- | +| Brute Force | O(nยฒ) | O(1) | +| Optimized | O(n) | O(1) | +| Best Approach | O(n) | O(1) | + +### ๐Ÿ“‰ Optimization Ideas + + You canโ€™t use hashing effectively here since the input is sorted, and space must be O(1). + + No need to explore binary search as two-pointer provides linear time with constant space. + +### ๐Ÿ“Œ Example Walkthroughs & Dry Runs + +Example 1: + +Input: [2, 7, 11, 15], target = 9 + +Initial pointers: +l = 0 โ†’ 2 +r = 3 โ†’ 15 + +Step 1: 2 + 15 = 17 โ†’ too big โ†’ r = 2 +Step 2: 2 + 11 = 13 โ†’ still big โ†’ r = 1 +Step 3: 2 + 7 = 9 โ†’ match found โ†’ return [1, 2] + +### ๐Ÿ”— Additional Resources + +- [Understanding Two Pointer Technique - GeeksForGeeks](https://www.geeksforgeeks.org/two-pointers-technique/) + + +Author: hanzel-sc +Date: 07/07/2025 \ No newline at end of file diff --git a/Arrays & Strings/Two Sum/Two Sum 2.c b/Arrays & Strings/Two Sum/Two Sum 2.c new file mode 100644 index 0000000..a82f716 --- /dev/null +++ b/Arrays & Strings/Two Sum/Two Sum 2.c @@ -0,0 +1,30 @@ + +int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) { + int l = 0; + int r = numbersSize-1; + + int* answer = (int*)malloc(2*sizeof(int)); //dynamic memory allocation + *returnSize = 2; //we're returning two values + + while (l < r) + { + int sum = (numbers[l] + numbers[r]); + if (sum == target) + { + answer[0] = l+1; //facilitating 1-based indexing as required by the problem. + answer[1] = r+1; + return answer; + } + else if (sum > target) + { + r--; //point to a smaller value to reduce the sum + } + else + { + l++; //point to a larger value to increase the sum + } + + } + return 0; + +}