diff --git a/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md new file mode 100644 index 0000000..62d67e8 --- /dev/null +++ b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md @@ -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 + diff --git a/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java new file mode 100644 index 0000000..97b6214 --- /dev/null +++ b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md new file mode 100644 index 0000000..9b160bd --- /dev/null +++ b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md @@ -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 + diff --git a/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java new file mode 100644 index 0000000..bfdf574 --- /dev/null +++ b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md new file mode 100644 index 0000000..150660d --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Explanation.md @@ -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 + diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp new file mode 100644 index 0000000..b36d0c5 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int searchInsert(vector& 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; + } +}; \ No newline at end of file diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java new file mode 100644 index 0000000..97b6214 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py new file mode 100644 index 0000000..ecc46c7 --- /dev/null +++ b/Arrays & Strings/#35 - Search Insert Position - Easy/searchInsertPosition - Solution.py @@ -0,0 +1,13 @@ +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + return mid + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file diff --git a/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md new file mode 100644 index 0000000..1c954ce --- /dev/null +++ b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md @@ -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 \ No newline at end of file diff --git a/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java new file mode 100644 index 0000000..f4e0ac4 --- /dev/null +++ b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java @@ -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); + } +} \ No newline at end of file