Solution - #50 - Hans - 09/04/2025#47
Conversation
SSexpl
left a comment
There was a problem hiding this comment.
The approach for both the questions is good , would just like you to add something about the naive solution ,atleast basic intitution and space and time complexity,
and in the explanation you can provide the Subheadings for Initution , algoithm https://github.com/Dijkstra-Edu/LeetCode-Solutions/blob/master/Explanation-Template.md
as stated here
|
I have updated the explanation with details requested and with respect to the template. Let me know if further changes have to be made? |
| 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 |
|
|
||
| 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; | ||
|
|
There was a problem hiding this comment.
Remove this, it has already been solved
JRS296
left a comment
There was a problem hiding this comment.
@hanzel-sc Remove the 2 sum related stuff and it can be merged
|
I've removed the repeated files. Let me know if anything else is needed |
|
Kindly resolve the issues in the conversation. The files still have not been removed @hanzel-sc |
|
@JRS296 I have removed the files. |
Problem: 50. Power(x,n)
Created a folder for Leetcode Math Solutions
Created a sub-folder for Power(x,n)
Files added:
Let me know if I have to make any changes :)