-
Notifications
You must be signed in to change notification settings - Fork 17
Solution - #50 - Hans - 09/04/2025 #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hanzel-sc remove this, it has already been solved
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this, it has already been solved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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 :)