𝗗𝗮𝘆 𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solve Merge Sorted Array using the Two Pointer (from end) technique. ➤ Approach (O(m + n), O(1) space): Set three pointers: —> i = m - 1 (end of nums1 valid elements) —> j = n - 1 (end of nums2) —> k = m + n - 1 (end of nums1 total capacity) Compare elements from the back and place the larger one at index k Move pointers accordingly until one array is exhausted ➤ Key Insight: Merging from the front would overwrite values. Merging from the back avoids extra space and keeps everything in-place. #LeetCode #Java #DSA #TwoPointers #ArrayProblems #ProblemSolving #20DaysChallenge #Consistency
Merge Sorted Array with Two Pointer Technique
More Relevant Posts
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Remove Element using the Two Pointer technique (in-place modification). ➤ Approach (O(n), O(1) space): • Use pointer i to track position of valid elements • Traverse array with pointer j • If nums[j] != val, copy it to nums[i] and increment i • Return i as the count of remaining elements No extra array. No shifting multiple times. Just overwrite unwanted values. ➤ Key Insight: Since order doesn’t matter after index k, we only care about keeping valid elements in the first part of the array. #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🚀 Day 30 / 100 | First Missing Positive -Intuition: The smallest missing positive will always be in range 1 to n+1. Place each number at its correct index (1 at index 0, 2 at index 1 so on). First index where value ≠ index+1 gives the missing number. -Approach : O(n) Find length of array n Traverse array and place each element at correct position -> index = value − 1 Ignore negative numbers, zero, and numbers > n After placement, traverse array again If arr[i] ≠ i+1 -> return i+1 If all elements are correct -> return n+1 -Complexity: Time Complexity : O(n) Space Complexity : O(1) #100DaysOfCode #Java #DSA #Array #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 32 of #100DaysOfCode Solved 4. Median of Two Sorted Arrays on LeetCode 📊 🧠 Key insight: Since both arrays are already sorted, we can merge them into one sorted array and directly compute the median. ⚙️ Approach: 🔹Merge the two sorted arrays into a single sorted array 🔹Find the middle index of the merged array 🔹If the total length is: 🔹Odd → median is the middle element 🔹Even → median is the average of the two middle elements ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(m + n) #100DaysOfCode #LeetCode #DSA #Arrays #MergeSort #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 29 of #100DaysOfCode Solved 442. Find All Duplicates in an Array on LeetCode ✅ 🧠 Key Insight: Since numbers are in the range 1 → n, each number ideally belongs at index num - 1. By repeatedly swapping elements into their correct positions (cyclic sort), duplicates naturally reveal themselves. ⚙️ Approach Used: 🔹Place each element at its correct index (nums[i] → nums[nums[i] - 1]) 🔹Skip when the element is already in the correct position 🔹After rearrangement, any index i where nums[i] ≠ i + 1 is a duplicate ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (excluding output list) #100DaysOfCode #LeetCode #DSA #Arrays #CyclicSort #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 20 - Find Lucky Integer in an Array Technique Used: HashMap Frequency Counting Key Idea: Constructed a frequency map to count occurrences of each element. Iterated through the map to identify elements whose value equals their frequency, and returned the maximum among them. Time Complexity: O(n) #Day20 #LeetCode #Java #DSA #HashMap #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 11 of #100DaysOfCode Solved Find Minimum in Rotated Sorted Array on LeetCode using Binary Search 🔄 🧠 Key insight: In a rotated sorted array, at least one half is always sorted. By comparing the middle element with the right boundary, we can determine which half contains the minimum and safely discard the other. ⚙️ Approach: 🔹Use binary search on the array 🔹Compare nums[mid] with nums[right] 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹Else → minimum lies in the left half (including mid) ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 36/100 – LeetCode Challenge 🚀 Problem: 3Sum Closest Approach: Sorted the array Fixed one element and applied the two-pointer technique Tracked the closest sum by comparing absolute differences Returned immediately if an exact match was found Time Complexity: O(n²) Space Complexity: O(1) Key takeaway: Many optimization problems are variations of classic patterns. Understanding 3Sum deeply makes solving 3Sum Closest straightforward. #LeetCode #100DaysOfCode #DSA #Java #TwoPointers #ProblemSolving #InterviewPrep
To view or add a comment, sign in
-
-
Today’s problem: Longest Substring with Exactly K Distinct Characters At first it looks like a brute-force substring problem, but that approach quickly drifts toward (O(n^2)). The key realization was that it follows the sliding window pattern. Key idea: Maintain a dynamic window and track character frequencies with a HashMap. Expand the window from the right If distinct characters exceed k, shrink from the left Update the answer only when the window contains exactly k distinct characters What clicked for me: This problem felt very similar to the Fruit Into Baskets problem I solved earlier. Once the pattern was clear, it became obvious that both problems use the same sliding-window template — only the constraint changes (2 types there vs k distinct characters here). Big takeaway: Many “different” substring problems are actually the same pattern with a small constraint change. Recognizing the pattern is half the solution. #GeeksForGeeks #ProblemOfTheDay #SlidingWindow #Java #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 45 / 100 | Longest Substring Without Repeating Characters Intuition: The problem is to find the length of the longest substring without repeating characters. If a repeated character appears, the substring must be adjusted to remove duplicates. Using a sliding window approach, it maintain a window of unique characters while scanning the string. Approach: O(n) -Use a HashSet to store characters currently in the substring window. -Maintain two pointers: left and right . -Iterate through the string using the right pointer. -If the character already exists in the set, remove characters from the left until the duplicate is removed. -Add the current character to the set. -Update the maximum length of the substring using the window size. -Repeat this process until the end of the string. Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development