Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Arrays & Strings/Two Sum/Explanation.md
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)
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

30 changes: 30 additions & 0 deletions Arrays & Strings/Two Sum/Two Sum 2.c
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;

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

}
177 changes: 177 additions & 0 deletions Math/Power(x,n)/Explanation.md
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
27 changes: 27 additions & 0 deletions Math/Power(x,n)/Power(x,n).c
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;
}