Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Problem Title: Container With Most Water

**Difficulty:** Medium
**Category:** Two Pointers, Greedy
**Leetcode Link:** https://leetcode.com/problems/container-with-most-water/

---

## 📝 Introduction

You're given an array `height[]` where each element represents the height of a vertical line on the x-axis. Picking any two lines forms a container that can trap water. The objective is to determine the **maximum possible water** that can be trapped.

---

## 💡 Approach & Key Insights

- Water trapped between two lines depends on:
- Width = (right - left)
- Height = minimum of the two heights
- To maximize area, we need a combination of good width and good height.
- Moving the pointer with the **smaller height** gives a chance to find a taller boundary, which could increase area.

---

## 🛠️ Breakdown of Approaches

### 1️⃣ Brute Force / Naive Approach

- **Explanation:**
- Check all possible pairs of lines `(i, j)`.
- Compute the area for each.
- Keep track of the highest.
- **Time Complexity:** O(n²)
- **Space Complexity:** O(1)
- **Example/Dry Run:**

Example: `height = [1, 8, 6]`
```
Pairs:
(0,1): width=1, minHeight=1 → area=1
(0,2): width=2, minHeight=1 → area=2
(1,2): width=1, minHeight=6 → area=6
Max = 6
```

---

### 2️⃣ Optimized Approach

- **Explanation:**
- Use two pointers starting at both ends.
- Compute area.
- Move the pointer with the lower height inward.
- Continue until left meets right.
- **Time Complexity:** O(n)
- **Space Complexity:** O(1)
- **Example/Dry Run:**

Example: `height = [1,8,6,2,5,4,8,3,7]`
```
l=0 (1), r=8 (7) → area=8 → move l
l=1 (8), r=8 (7) → area=49 → move r
l=1 (8), r=7 (3) → area=18 → move r
l=1 (8), r=6 (8) → area=40 → move r
...
Max area = 49
```

---

### 3️⃣ Best / Final Optimized Approach

- The two-pointer solution is already optimal.
- No better time complexity exists for this problem.

---

## 📊 Complexity Analysis

| Approach | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Brute Force | O(n²) | O(1) |
| Optimized | O(n) | O(1) |
| Best Approach | O(n) | O(1) |

---

## 📉 Optimization Ideas

- You can't beat O(n), but you can:
- Improve pointer movement logic for readability.
- Add early exits if certain patterns are detected.

---

## 📌 Example Walkthroughs & Dry Runs

```plaintext
Example:
Input: [4,3,2,1,4]
- (0,4): width=4, height=4 → area=16
- Next comparisons produce smaller areas
Output: 16
```

```plaintext
Example:
Input: [1,2,1]
- (0,2): width=2, height=1 → area=2
- (1,2): width=1, height=1 → area=1
Output: 2
```

---

## 🔗 Additional Resources

- Two Pointer Techniques
- Greedy Strategy Explanations
- LeetCode Official Discuss Section

---

Author:
Date: 18/11/2025

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Problem Title: Concatenation of Array

**Difficulty:** Easy
**Category:** Array, Simulation
**LeetCode Link:** https://leetcode.com/problems/concatenation-of-array/

---

## 📝 Introduction
You're given an integer array `nums`. Your task is to create a new array `ans` of size `2 * n` where:

```
ans[i] = nums[i]
ans[i + n] = nums[i]
```

In short: **Just stick the array to itself.** Yup, it’s literally copy–paste.

---

## 💡 Approach & Key Insights
- This is a straightforward construction problem.
- No tricks, no traps, no two pointers pretending to be deep.
- Just build a new array where the second half repeats the first.

---

## 🛠️ Breakdown of Approaches

### 1️⃣ Brute Force (Almost too simple to be called brute force)
- Create a new array of size `2n`.
- First loop: fill `ans[0..n-1]` with `nums`.
- Second loop: fill `ans[n..2n-1]` with `nums` again.
- **Time Complexity:** O(n)
- **Space Complexity:** O(n)

### 2️⃣ Optimized / Best Approach
- Just do: `ans = nums + nums` (in languages that allow it).
- Or use array copy utilities.
- Still **O(n)** time and space — but cleaner code.

There’s no further optimization possible here… unless you bully the interviewer.

---

## 📊 Complexity Analysis

| Approach | Time Complexity | Space Complexity |
|--------------|----------------|------------------|
| Standard | O(n) | O(n) |
| Best Approach| O(n) | O(n) |

---

## 📉 Example / Dry Run

### Example 1:
```
Input: nums = [1,2,1]

ans:
Index: 0 1 2 3 4 5
Value: 1 2 1 1 2 1

Output: [1,2,1,1,2,1]
```

### Example 2:
```
Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
```

---

## 🔗 Additional Notes
- This problem is typically used to warm up or check basic array manipulation.
- If this feels too easy — good. It was supposed to.

---

Author:
Date: 18/11/2025

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int[] getConcatenation(int[] nums) {
int n = nums.length;
int[] ans = new int[n * 2];
for(int i = 0; i < n; i++){
ans[i] = nums[i];
ans[i + n] = nums[i];
}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Problem Title

**Difficulty:** Easy
**Category:** Binary Search, Arrays
**Leetcode Link:** [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/)

---

## 📝 Introduction

Given a sorted array of **distinct integers** and a target value, the task is to return the **index** if the target is found. If not, return the index where it **would be inserted** in order.

This is a **classic binary search** problem with a small twist — instead of just finding the element, we also handle where it would fit in if it’s not present.

---

## 💡 Approach & Key Insights

- The array is sorted — so binary search is the best tool here (O(log n) time).
- We look for the **first position** where `nums[i] >= target`.
- If the target is greater than all elements, it should be inserted at the **end** (index = `len(nums)`).
- This ensures correct placement whether or not the target exists in the array.

---

## 🛠️ Breakdown of Approaches

### 1️⃣ Brute Force / Naive Approach

- **Explanation:**
- Iterate through the array.
- Return the index where `nums[i] >= target`.
- If target is larger than all elements, return `len(nums)`.
- **Time Complexity:** O(n) — linear scan through the array.
- **Space Complexity:** O(1) — no extra memory.
- **Example/Dry Run:**

Example input: `nums = [1, 3, 5, 6], target = 2`

```
Index 0: 1 < 2 → continue
Index 1: 3 >= 2 → return 1
Output: 1
```

---

### 2️⃣ Optimized Approach

- **Explanation:**
- Use binary search to narrow down where the element should be.
- Maintain `left` and `right` pointers.
- If `nums[mid] == target`, return `mid`.
- If `nums[mid] < target`, move `left = mid + 1`.
- Else move `right = mid - 1`.
- When loop ends, `left` will represent the **insert position**.
- **Time Complexity:** O(log n) — binary search.
- **Space Complexity:** O(1) — constant space.
- **Example/Dry Run:**

Example input: `nums = [1, 3, 5, 6], target = 5`

```
left = 0, right = 3
mid = 1 (value = 3) → 3 < 5 → left = 2
mid = 2 (value = 5) → match → return 2
```

Output: 2

---

## 📊 Complexity Analysis

| Approach | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Brute Force | O(n) | O(1) |
| Optimized | O(log n) | O(1) |

---

## 📉 Optimization Ideas

- Using **binary search built-ins** like `bisect_left` in Python can reduce boilerplate.
- For small arrays (n < 10), linear scan may be equally efficient — but binary search is best for scalability.

---

## 📌 Example Walkthroughs & Dry Runs

```plaintext
Example 1:
nums = [1,3,5,6], target = 5
→ Found at index 2

Example 2:
nums = [1,3,5,6], target = 2
→ Insert before 3 → index 1

Example 3:
nums = [1,3,5,6], target = 7
→ Beyond all → insert at end → index 4
```

---

## 🔗 Additional Resources

- [Binary Search (GeeksforGeeks)](https://www.geeksforgeeks.org/binary-search/)
- [Python bisect module](https://docs.python.org/3/library/bisect.html)
- [Leetcode Binary Search Patterns](https://leetcode.com/explore/learn/card/binary-search/)

---

Author:
Date: 11/11/2025

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
}
Loading