Merging Two Sorted Arrays with Extra Space

🚀 Day 21 of #DevDSA 🔹 Problem: Merge Two Sorted Arrays (Using Extra Space) Today I worked on merging two sorted arrays into a single sorted array using an additional array. 🧠 Approach: Used two pointers (l for nums1 and r for nums2) Compared elements one by one Pushed the smaller element into a new mergedArray Handled equal elements by pushing both Continued until one array is exhausted 💡 Key Idea: Since both arrays are already sorted, we can efficiently merge them in linear time without sorting again. ⏱ Time Complexity: O(m + n) 📦 Space Complexity: O(m + n) 📌 What I learned: Two-pointer technique is very powerful for sorted data Writing clean conditions avoids unnecessary complexity Always think about edge cases (like remaining elements after loop) 💻 Next Step: Try solving the same problem without extra space (in-place optimization 🔥) #DSA #100DaysOfCode #CodingJourney #JavaScript #InterviewPrep

  • text

To view or add a comment, sign in

Explore content categories