-
Notifications
You must be signed in to change notification settings - Fork 23
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
hanzel-sc
wants to merge
6
commits into
Dijkstra-Edu:master
Choose a base branch
from
hanzel-sc:math-power-c
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c289acf
Commit #123 - Added Two Sum II solution and explanation in C - 07.04.…
hanzel-sc 7f162c5
Solution #50 - Hans - 9.04.25 - Commit#124
hanzel-sc a8fdf74
Updated explanation file in accordance with review
hanzel-sc 70e9ce1
Updated explanation file according to template
hanzel-sc d124a43
Deleted unneccesary files
hanzel-sc 6f34401
Deleted unneccesary files
hanzel-sc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
|
||
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| # 🚀 Problem 50: Power (x, n) | ||
|
|
||
| **Difficulty:** Medium | ||
| **Category:** Math | ||
| **Leetcode Link:** [https://leetcode.com/problems/powx-n/](https://leetcode.com/problems/powx-n/) | ||
|
|
||
| --- | ||
|
|
||
| ### 📝 Introduction | ||
|
|
||
| Implement the function `pow(x, n)`, which computes `x` raised to the power `n` (i.e., `xⁿ`) without using built-in functions like `pow()`. | ||
| Constraints include: | ||
| - The exponent `n` can be negative. | ||
| - You must handle large powers efficiently. | ||
| - Result should be a double. | ||
|
|
||
| --- | ||
|
|
||
| ### 💡 Approach & Key Insights | ||
|
|
||
| At first, this seems straightforward — just multiply `x` by itself `n` times. However, such a naive approach fails on large values of `n` due to time constraints. | ||
|
|
||
| The optimized solution uses **Binary Exponentiation**, which brings the time complexity down to **O(log n)** by reducing the number of multiplications required. | ||
|
|
||
| We also need to handle edge cases like: | ||
| - Any number to the power of 0 is 1. | ||
| - 1 raised to any power is 1. | ||
| - Negative exponents require us to take the reciprocal (`x^-n = 1 / x^n`). | ||
|
|
||
| --- | ||
|
|
||
| ### 🛠️ Breakdown of Approaches | ||
|
|
||
| --- | ||
|
|
||
| #### 1️⃣ Brute Force / Naive Approach | ||
|
|
||
| **Explanation:** | ||
| Multiply `x` by itself `n` times using a loop. | ||
|
|
||
| ```c | ||
| double myPow(double x, int n) { | ||
| double result = 1; | ||
| for (int i = 0; i < n; i++) { | ||
| result *= x; | ||
| } | ||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| **Time Complexity:** O(n) — Each multiplication is done one by one. | ||
| **Space Complexity:** O(1) — No extra space except result variable. | ||
|
|
||
| **Example / Dry Run:** | ||
|
|
||
| Input: `x = 2`, `n = 4` | ||
| Steps: | ||
| 2 × 2 = 4 → 4 × 2 = 8 → 8 × 2 = 16 | ||
| **Output:** `16` | ||
|
|
||
| *⚠️ This approach fails for large `n` due to TLE on LeetCode.* | ||
|
|
||
| --- | ||
|
|
||
| #### 2️⃣ Optimized Approach — Binary Exponentiation | ||
|
|
||
| **Explanation:** | ||
| We use the property of powers: | ||
|
|
||
| - If `n` is even: | ||
| `xⁿ = (x^(n/2))²` | ||
| - If `n` is odd: | ||
| `xⁿ = x × xⁿ⁻¹` | ||
|
|
||
| We convert `n` to a **positive long long** if it's negative and take the **reciprocal** at the end for negative exponents. | ||
|
|
||
| **Edge Cases to Handle:** | ||
| - `x^0 = 1` | ||
| - `1^n = 1` | ||
| - `x^-n = 1 / x^n` | ||
|
|
||
| **C Code Snippet:** | ||
|
|
||
| ```c | ||
| double myPow(double x, int n) { | ||
| long long z = n; | ||
| double result = 1.0; | ||
|
|
||
| if (z < 0) { | ||
| x = 1 / x; | ||
| z = -z; | ||
| } | ||
|
|
||
| while (z > 0) { | ||
| if (z % 2 == 1) { | ||
| result *= x; | ||
| z--; | ||
| } else { | ||
| x *= x; | ||
| z /= 2; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| **Time Complexity:** O(log n) — At each step, we reduce the exponent by half. | ||
| **Space Complexity:** O(1) — Just a few variables. | ||
|
|
||
| **Example / Dry Run:** | ||
|
|
||
| Input: `x = 2`, `n = 8` | ||
| Steps: | ||
| - 2^8 = (2^4)^2 | ||
| - 2^4 = (2^2)^2 | ||
| - 2^2 = (2^1)^2 | ||
| - 2^1 = 2 | ||
|
|
||
| So: | ||
| 2 → 4 → 16 → 256 | ||
| **Output:** `256` | ||
|
|
||
| --- | ||
|
|
||
| ### 📊 Complexity Analysis | ||
|
|
||
| | Approach | Time Complexity | Space Complexity | | ||
| |------------------|------------------|-------------------| | ||
| | Brute Force | O(n) | O(1) | | ||
| | Optimized | O(log n) | O(1) | | ||
|
|
||
| --- | ||
|
|
||
| ### 📉 Optimization Ideas | ||
|
|
||
| - Ensure you're casting `n` to `long long` when handling `INT_MIN`, since `-INT_MIN` would overflow in 32-bit int. | ||
| - Use **bit manipulation** if desired (e.g., `z >> 1` instead of `z / 2`) for micro-optimizations. | ||
|
|
||
| --- | ||
|
|
||
| ### 📌 Example Walkthroughs & Dry Runs | ||
|
|
||
| **Example 1:** | ||
| ``` | ||
| Input: x = 2, n = 5 | ||
|
|
||
| Step 1: 2^5 = 2 * 2^4 | ||
| Step 2: 2^4 = (2^2)^2 = (4)^2 = 16 | ||
| Step 3: Result = 2 * 16 = 32 | ||
| Output: 32 | ||
| ``` | ||
|
|
||
| **Example 2:** | ||
| ``` | ||
| Input: x = 2.0, n = -3 | ||
|
|
||
| Step 1: Invert x → x = 1 / 2.0 = 0.5, n = 3 | ||
| Step 2: 0.5^3 = 0.5 * 0.5^2 | ||
| Step 3: 0.5^2 = (0.5)^2 = 0.25 | ||
| Step 4: 0.5 * 0.25 = 0.125 | ||
| Output: 0.125 | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 🔗 Additional Resources | ||
|
|
||
| - [Power(x, n) - NeetCode Video](https://www.youtube.com/watch?v=l0YC3876qxg) | ||
| - [Binary Exponentiation - CP Algorithms](https://cp-algorithms.com/algebra/binary-exp.html) | ||
| - [Understanding Power Function - GeeksForGeeks](https://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/) | ||
|
|
||
| --- | ||
|
|
||
| **Author:** hanzel-sc | ||
|
|
||
| **Date:** 07/07/2025 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| double myPow(double x, int n) { | ||
| double y; | ||
| int i; | ||
| long z = n; //converting to long to avoid the integer overflow/limt | ||
| y = 1; | ||
| if (x == 1 || n == 0) //x^0 == 1 and 1^n == 1 | ||
| return 1; | ||
|
|
||
| else if (z<0) //for negative numbers | ||
| { | ||
| z = z * -1; | ||
| x = 1/x; //x^-1 == 1/x^n :) | ||
| } | ||
| //using the concept of binary exponentiation | ||
| while (z>0) | ||
| { | ||
| if (z%2==1) | ||
| { | ||
| y = y * x; // y keeps track of the result in every step | ||
| z = z-1; | ||
| } | ||
|
|
||
| x = x*x; | ||
| z = z/2; | ||
| } | ||
| return y; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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