From c289acfff9e51bf74b84d6e1b75d63737a8c4cc6 Mon Sep 17 00:00:00 2001 From: hanzel_sc Date: Mon, 7 Apr 2025 14:02:17 +0530 Subject: [PATCH 1/3] Commit #123 - Added Two Sum II solution and explanation in C - 07.04.2025 --- Arrays & Strings/Two Sum/Explanation.md | 19 ++++++++++++++++ Arrays & Strings/Two Sum/Two Sum 2.c | 30 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Arrays & Strings/Two Sum/Explanation.md create mode 100644 Arrays & Strings/Two Sum/Two Sum 2.c diff --git a/Arrays & Strings/Two Sum/Explanation.md b/Arrays & Strings/Two Sum/Explanation.md new file mode 100644 index 0000000..a675f3b --- /dev/null +++ b/Arrays & Strings/Two Sum/Explanation.md @@ -0,0 +1,19 @@ +Explanation (C solution): + +Two Sum II, a slightly modified version of Two Sum I where we get a sorted array. +The solution's straightforward with the utilization of two pointers - at the start and end of the array respectively, let's say l and r. +We move inwards into the array until our start and end pointers don't cross over i.e left > right. +We check if the value at array[l] + array[r] (which is our sum) == target. + +Since it's a sorted array. If: +1. Sum is greater than Target +-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..). +2. Sum is lesser than Target +-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..). + +If Sum is equal to our target: +-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) +-We've increased the index by 1 to facilitate 1-based indexing as given in the problem. +-Return the malloced array. + +Time Complexity: O(n) \ 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; + +} From b378f52b5c33a5b42aae93f2f83a05444f3fbeb3 Mon Sep 17 00:00:00 2001 From: hanzel_sc Date: Sun, 6 Jul 2025 02:56:34 +0530 Subject: [PATCH 2/3] Updated explanation with necessary changes --- Arrays & Strings/Two Sum/Explanation.md | 46 +++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/Arrays & Strings/Two Sum/Explanation.md b/Arrays & Strings/Two Sum/Explanation.md index a675f3b..7145910 100644 --- a/Arrays & Strings/Two Sum/Explanation.md +++ b/Arrays & Strings/Two Sum/Explanation.md @@ -1,7 +1,33 @@ Explanation (C solution): -Two Sum II, a slightly modified version of Two Sum I where we get a sorted array. -The solution's straightforward with the utilization of two pointers - at the start and end of the array respectively, let's say l and r. +## Two Sum II - Input Array is Sorted + +### Difficulty : Easy +### Category : Arrays, Two pointer + +Leetcode Link : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ + +### Introduction + +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. + +### Intuition and Naive Approach/Brute Force + +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. + +for(int i=0; i < n; i++) { + for(int j=i+1; j < n; j++){ + if (num[i] + num[j] == target) + return [i+1, j+1] + } +} + +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) + +Space complexity: O(1) + +### Optimized Solution +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. We move inwards into the array until our start and end pointers don't cross over i.e left > right. We check if the value at array[l] + array[r] (which is our sum) == target. @@ -16,4 +42,18 @@ If Sum is equal to our target: -We've increased the index by 1 to facilitate 1-based indexing as given in the problem. -Return the malloced array. -Time Complexity: O(n) \ No newline at end of file +Time Complexity: O(n) +Space complexity: O(1) + +### Example +Numbers = [2, 7, 11, 15], target = 9 + +The working : + +l = 0, r = 3 → 2 + 15 = 17 → sum larger than target, reduce right pointer + +l = 0, r = 2 → 2 + 11 = 13 → sum larger than target, reduce right pointer + +l = 0, r = 1 → 2 + 7 = 9 → sum equal → return [1, 2] + +Output: [1, 2] \ No newline at end of file From d7884d153e4beeb15653e9226ff12984fe1eb6dc Mon Sep 17 00:00:00 2001 From: hanzel_sc Date: Mon, 7 Jul 2025 00:53:06 +0530 Subject: [PATCH 3/3] Updated explanation file according to template --- Arrays & Strings/Two Sum/Explanation.md | 132 +++++++++++++++++------- 1 file changed, 94 insertions(+), 38 deletions(-) diff --git a/Arrays & Strings/Two Sum/Explanation.md b/Arrays & Strings/Two Sum/Explanation.md index 7145910..786375a 100644 --- a/Arrays & Strings/Two Sum/Explanation.md +++ b/Arrays & Strings/Two Sum/Explanation.md @@ -1,59 +1,115 @@ -Explanation (C solution): +## 🧮 Two Sum II - Input Array Is Sorted -## 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/ -### Difficulty : Easy -### Category : Arrays, Two pointer +### 📝 Introduction -Leetcode Link : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ +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. -### Introduction +### 💡 Approach & Key Insights -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. + Key Insight: Since the array is sorted, we can use the two-pointer technique instead of brute force. -### Intuition and Naive Approach/Brute Force + Naive approach checks all pairs — this is simple but inefficient (O(n²)). -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. + Optimized approach uses two pointers (start and end) to shrink the search space intelligently in O(n) time. -for(int i=0; i < n; i++) { - for(int j=i+1; j < n; j++){ - if (num[i] + num[j] == target) - return [i+1, j+1] - } -} + We return a dynamically allocated array in C to store the result as C functions can’t directly return multiple values. -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) +### 🛠️ Breakdown of Approaches -Space complexity: O(1) +### 1️⃣ Brute Force / Naive Approach -### Optimized Solution -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. -We move inwards into the array until our start and end pointers don't cross over i.e left > right. -We check if the value at array[l] + array[r] (which is our sum) == target. +Explanation: +Check every pair (i, j) where i < j. If numbers[i] + numbers[j] == target, return their 1-based indices. -Since it's a sorted array. If: -1. Sum is greater than Target --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..). -2. Sum is lesser than Target --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..). +Time Complexity: O(n²) +Each element is paired with every other element ahead of it. -If Sum is equal to our target: --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) --We've increased the index by 1 to facilitate 1-based indexing as given in the problem. --Return the malloced array. +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) -Space complexity: O(1) +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 -### Example -Numbers = [2, 7, 11, 15], target = 9 +Initial pointers: +l = 0 → 2 +r = 3 → 15 -The working : +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] -l = 0, r = 3 → 2 + 15 = 17 → sum larger than target, reduce right pointer +### 🔗 Additional Resources -l = 0, r = 2 → 2 + 11 = 13 → sum larger than target, reduce right pointer +- [Understanding Two Pointer Technique - GeeksForGeeks](https://www.geeksforgeeks.org/two-pointers-technique/) -l = 0, r = 1 → 2 + 7 = 9 → sum equal → return [1, 2] -Output: [1, 2] \ No newline at end of file +Author: hanzel-sc +Date: 07/07/2025 \ No newline at end of file