Skip to content

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

hanzel-sc
Copy link
Member

Problem: 50. Power(x,n)

Created a folder for Leetcode Math Solutions

Created a sub-folder for Power(x,n)

Files added:

  1. Power(x,n) solution in C
  2. Explanation.md : Explanation file with an explanation specific to C.

Let me know if I have to make any changes :)

@JRS296 JRS296 requested a review from SSexpl May 10, 2025 06:09
Copy link
Member

@SSexpl SSexpl left a 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

@hanzel-sc
Copy link
Member Author

I have updated the explanation with details requested and with respect to the template. Let me know if further changes have to be made?

Comment on lines 1 to 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)
Copy link
Member

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

Comment on lines 1 to 29

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;

Copy link
Member

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

Copy link
Member

@JRS296 JRS296 left a 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

@hanzel-sc
Copy link
Member Author

hanzel-sc commented Jul 19, 2025

I've removed the repeated files. Let me know if anything else is needed

@JRS296
Copy link
Member

JRS296 commented Jul 20, 2025

Kindly resolve the issues in the conversation. The files still have not been removed @hanzel-sc

@hanzel-sc
Copy link
Member Author

@JRS296 I have removed the files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants