<?xml version="1.0"?>
<rss version="2.0"><channel><title><![CDATA[Coding Challenges & Algorithms]]></title><link>https://www.thecrimsonmarket.com/hub/blogs/blog/8-coding-challenges-algorithms/</link><description><p>
	Sharpen your problem-solving skills with engaging coding challenges and in-depth algorithm breakdowns. From beginner-friendly puzzles to advanced competitive programming problems, this section helps you improve logic, optimize code efficiency, and master data structures.
</p>
</description><language>en</language><item><title>Leet code 88 - Merging Two Sorted Arrays in JavaScript</title><link>https://www.thecrimsonmarket.com/hub/blogs/entry/8-leet-code-88-merging-two-sorted-arrays-in-javascript/</link><description><![CDATA[<h3>
	<span style="color:#ecf0f1;"><strong>Merging Two Sorted Arrays in JavaScript</strong></span>
</h3>

<p>
	<span style="color:#ecf0f1;">One of the most common tasks you will encounter in algorithmic challenges is merging two sorted arrays. This concept might seem simple at first glance, but it becomes more interesting when constraints are added. For example, in this challenge, we need to merge two sorted arrays in-place in the first array while maintaining a time complexity of <strong>O(m + n)</strong>.</span>
</p>

<p>
	<span style="color:#ecf0f1;">But why is this problem important, and how does it relate to real-world coding scenarios?</span>
</p>

<h3>
	<span style="color:#ecf0f1;"><strong>Why Merging Sorted Arrays Matters</strong></span>
</h3>

<p>
	<span style="color:#ecf0f1;">Merging sorted arrays efficiently is crucial in many areas of software development. Whether you're building an algorithm that needs to combine multiple streams of sorted data (think of combining search results from different sources), or you're working on a system that processes large amounts of sorted data, mastering this problem will help you optimize performance and reduce memory usage.</span>
</p>

<p>
	<span style="color:#ecf0f1;">Additionally, many modern web applications rely on manipulating large datasets. Being able to handle merging operations quickly and without extra memory allocation is an essential skill for building efficient systems.</span>
</p>

<p>
	<span style="color:#ecf0f1;">Before we dive into the solution, <strong>give it a go yourself</strong>! Can you come up with an efficient algorithm to merge two sorted arrays? Think about the constraints of doing this in-place.</span>
</p>

<h3>
	<span style="color:#ecf0f1;"><strong>Challenge: Merging Sorted Arrays</strong></span>
</h3>

<p>
	<span style="color:#ecf0f1;">If you’re looking for the original challenge, here’s a link to the problem: </span><a rel=""><span style="color:#ecf0f1;">LeetCode 88: Merge Sorted Array</span></a><span style="color:#ecf0f1;">.</span>
</p>

<p>
	<span style="color:#ecf0f1;">Take some time to solve the problem. We'll wait here while you give it a shot!</span>
</p>

<h3>
	<span style="color:#ecf0f1;"><strong>Our Approach to the Problem</strong></span>
</h3>

<p>
	<span style="color:#ecf0f1;">Alright, if you've tried the challenge and are ready for the solution, here’s a step-by-step breakdown of our approach:</span>
</p>

<ol>
	<li>
		<p>
			<span style="color:#ecf0f1;"><strong>Two-Pointer Approach</strong>: We use two pointers, one for each of the two arrays. We start from the end of each array and compare the elements. The reason for starting at the end is that it allows us to fill the array <code>nums1</code> from the back, ensuring we don’t overwrite elements that have not been processed yet.</span>
		</p>
	</li>
	<li>
		<p>
			<span style="color:#ecf0f1;"><strong>In-place Merging</strong>: The challenge specifies that the result must be stored in <code>nums1</code>, which is already preallocated with enough space to hold the merged result. So, rather than creating a new array, we modify <code>nums1</code> in place to save memory. This is a key part of the problem.</span>
		</p>
	</li>
	<li>
		<p>
			<span style="color:#ecf0f1;"><strong>Efficient Time Complexity</strong>: We aim for <strong>O(m + n)</strong> time complexity, where <code>m</code> is the number of elements in <code>nums1</code> and <code>n</code> is the number of elements in <code>nums2</code>. This is achieved because each element from both arrays is processed exactly once.</span>
		</p>
	</li>
	<li>
		<p>
			<span style="color:#ecf0f1;"><strong>Handling Remaining Elements</strong>: Once we’ve finished comparing the elements from the two arrays, there might still be elements left in <code>nums2</code>. If that’s the case, we simply copy them into <code>nums1</code> since they are already sorted.</span>
		</p>
	</li>
</ol>

<p>
	<span style="color:#ecf0f1;">Here’s the code for our solution in <strong>Node.js</strong>:</span>
</p>

<pre> </pre>

<div>
	<div>
		<span style="color:#ecf0f1;">javascript</span>
	</div>

	<div>
		<div>
			<div>
				<span style="color:#ecf0f1;"><span>Copy</span><span>Edit</span></span>
			</div>
		</div>
	</div>

	<div dir="ltr">
		<span style="color:#ecf0f1;"><code>/** * Merges two sorted arrays into the first array in sorted order. * The first array nums1 has enough space to hold all elements from both arrays. * * @param {number[]} nums1 - The first sorted array with extra space at the end. * @param {number} m - The number of valid elements in nums1. * @param {number[]} nums2 - The second sorted array. * @param {number} n - The number of valid elements in nums2. */ function mergeSortedArrays(nums1, m, nums2, n) { let idx1 = m - 1; // Pointer for the last element in the valid part of nums1 let idx2 = n - 1; // Pointer for the last element in nums2 let mergeIdx = m + n - 1; // Pointer for the last position in nums1 (where we merge elements) // Merge the arrays from the back to avoid overwriting elements in nums1 while (idx1 &gt;= 0 &amp;&amp; idx2 &gt;= 0) { if (nums1[idx1] &gt; nums2[idx2]) { nums1[mergeIdx] = nums1[idx1]; idx1--; } else { nums1[mergeIdx] = nums2[idx2]; idx2--; } mergeIdx--; } // If there are any remaining elements in nums2, copy them over while (idx2 &gt;= 0) { nums1[mergeIdx] = nums2[idx2]; idx2--; mergeIdx--; } } // Example usage const nums1 = [1, 2, 3, 0, 0, 0]; const m = 3; const nums2 = [2, 5, 6]; const n = 3; mergeSortedArrays(nums1, m, nums2, n); console.log(nums1); // Output: [1, 2, 2, 3, 5, 6] </code></span>
	</div>
</div>

<h3>
	<span style="color:#ecf0f1;"><strong>Key Takeaways</strong>:</span>
</h3>

<ul>
	<li>
		<span style="color:#ecf0f1;"><strong>Two-pointer technique</strong>: Start merging from the end of the arrays to avoid overwriting elements.</span>
	</li>
	<li>
		<span style="color:#ecf0f1;"><strong>In-place merge</strong>: We don’t need extra memory space, making the solution more efficient.</span>
	</li>
	<li>
		<span style="color:#ecf0f1;"><strong>Efficient time complexity</strong>: The algorithm runs in <strong>O(m + n)</strong> time, which is optimal for this problem.</span>
	</li>
</ul>

<h3>
	<span style="color:#ecf0f1;"><strong>Your Turn!</strong></span>
</h3>

<p>
	<span style="color:#ecf0f1;">Now that you've seen the solution, it's time to <strong>test your skills</strong>. Try running the code with different inputs and see how quickly you can modify the arrays! Also, if you found this blog helpful, we’d love to hear from you.</span>
</p>

<h3>
	<span style="color:#ecf0f1;">Let’s get the conversation going! <strong>Post your results in the chat</strong> and tell us how you solved the challenge or if you found any alternative approaches. We're excited to see how you tackled it!</span>
</h3>

<p>
	<span style="color:#ecf0f1;">Happy coding! <span class="ipsEmoji">💻</span><span class="ipsEmoji">🚀</span></span>
</p>
]]></description><guid isPermaLink="false">8</guid><pubDate>Fri, 14 Feb 2025 09:31:20 +0000</pubDate></item></channel></rss>
