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,128 @@
# Problem Title: Max Consecutive Ones

**Difficulty:** Easy
**Category:** Arrays
**Leetcode Link:** https://leetcode.com/problems/max-consecutive-ones/

---

## 📝 Introduction

You're given a binary array `nums` (only 0s and 1s). Your task is to find the **maximum number of consecutive 1s** in the array.

Deceptively simple on the surface, but a perfect warm-up problem to build sliding window intuition.

---

## 💡 Approach & Key Insights

- We want the **longest streak** of consecutive 1s.
- Every time we hit a `0`, the streak ends.
- The simplest way: use a running counter for current streak and a max counter to store the best streak seen.

This is extremely efficient and requires just one pass.

---

## 🛠️ Breakdown of Approaches

### 1️⃣ Brute Force / Naive Approach

- **Explanation:**
- Check each starting index.
- Count how many 1s follow it.
- Track the maximum.
- Works, but extremely inefficient.
- **Time Complexity:** O(n²)
- **Space Complexity:** O(1)
- **Example/Dry Run:**

Example: `[1,1,0,1]`
```
Start at index 0 → streak = 2
Start at index 1 → streak = 1
Start at index 2 → streak = 0
Start at index 3 → streak = 1
Max = 2
```

---

### 2️⃣ Optimized Approach

- **Explanation:**
- Traverse the array once.
- Keep:
- `currentCount` → current streak of 1s
- `maxCount` → best streak found
- Reset `currentCount` to 0 whenever you encounter a 0.
- **Time Complexity:** O(n)
- **Space Complexity:** O(1)
- **Example/Dry Run:**

Example: `[1,1,0,1,1,1]`
```
1 → current=1, max=1
1 → current=2, max=2
0 → current=0
1 → current=1, max=2
1 → current=2, max=2
1 → current=3, max=3
Final answer = 3
```

---

### 3️⃣ Best / Final Optimized Approach

- There is no faster solution than O(n) — the optimized pass is already the final method.

---

## 📊 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

- If the array is extremely large and memory-mapped, streaming evaluation still works due to O(1) space.
- Useful trick: whenever you see a problem involving **consecutive streaks**, think linear scan + counters first.

---

## 📌 Example Walkthroughs & Dry Runs

```plaintext
Example:
Input: [0,1,1,1,0,1,1]
Process:
- Streaks: 3 and 2
Output: 3
```

```plaintext
Example:
Input: [1,1,1,1]
Process:
- Entire array is a streak of 4
Output: 4
```

---

## 🔗 Additional Resources

- Sliding Window Basics
- Counting Techniques in Arrays
- LeetCode Discuss Thread

---

Author:
Date: 18/11/2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if it can handle null and empty input.
Math.max is called for every zero.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int tempMax = 0;
for(int i = 0 ; i< nums.length; i++){
if(nums[i] == 1) max++;
if(nums[i] == 0){
tempMax = Math.max(tempMax, max);
max = 0;
}
}
return Math.max(tempMax, max);
}
}