{"id":525,"date":"2025-07-07T18:55:59","date_gmt":"2025-07-07T13:25:59","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=525"},"modified":"2025-07-07T18:56:01","modified_gmt":"2025-07-07T13:26:01","slug":"merge-without-extra-space","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/","title":{"rendered":"Merge Without\u00a0Extra Space \u2013\u00a0GeeksforGeeks Solution\u00a0Explained"},"content":{"rendered":"\n<p>If you want to master array manipulation, &#8220;Merge Without Extra Space&#8221; is an essential coding problem! In this blog, we\u2019ll explain the problem, show you both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Given two sorted arrays&nbsp;<strong>a[]<\/strong>&nbsp;and&nbsp;<strong>b[]&nbsp;<\/strong>of size&nbsp;<strong>n&nbsp;<\/strong>and&nbsp;<strong>m&nbsp;<\/strong>respectively, the task is to merge them in sorted order without using any&nbsp;<strong>extra space<\/strong>. Modify&nbsp;<strong>a[]<\/strong>&nbsp;so that it contains the first&nbsp;<strong>n<\/strong>&nbsp;elements and modify&nbsp;<strong>b[]<\/strong>&nbsp;so that it contains the last&nbsp;<strong>m<\/strong>&nbsp;elements.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/merge-two-sorted-arrays-1587115620\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input<\/strong>: a[] = [2, 4, 7, 10], b[] = [2, 3]\n<strong>Output<\/strong>:<br>2 2 3 4<br>7 10\n<strong>Explanation<\/strong>: After merging the two non-decreasing arrays, we get, 2 2 3 4 7 10<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input<\/strong>: a[] = [1, 5, 9, 10, 15, 20], b[] = [2, 3, 8, 13]\n<strong>Output<\/strong>:<br>1 2 3 5 8 9<br>10 13 15 20\n<strong>Explanation<\/strong>: After merging two sorted arrays we get 1 2 3 5 8 9 10 13 15 20.\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input<\/strong>: a[] = [0, 1], b[] = [2, 3]\n<strong>Output<\/strong>:<br>0 1<br>2 3\n<strong>Explanation<\/strong>: After merging two sorted arrays we get 0 1 2 3.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><br>1 &lt;= a.size(), b.size() &lt;= 10<sup>5<\/sup><br>0 &lt;= a[i], b[i]&nbsp;&lt;= 10<sup>7<\/sup><\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-5aa96890-8295-4f3b-b3b4-dd9f5071d6e5\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#0-brute-force-solution\" style=\"\">Brute Force Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#3-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#4-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#5-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#6-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#7-optimal-solution\" style=\"\">Optimal Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#10-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#11-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#12-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#13-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/merge-without-extra-space\/#14-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-brute-force-solution\">Brute Force Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem step by step in the simplest way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Merge into a Third Array:<\/strong><br>Use a new array to merge both arrays like in merge sort.<\/li>\n\n\n\n<li><strong>Copy Back:<\/strong><br>After merging, copy the first part back to\u00a0<code>arr1<\/code>\u00a0and the remaining to\u00a0<code>arr2<\/code>.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We use extra space to make merging easy, then distribute the sorted elements back.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but does not meet the &#8220;no extra space&#8221; requirement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\nclass Solution:\n    def mergeArrays(self, arr1, arr2):\n        n, m = len(arr1), len(arr2)\n        arr3 = [0] * (n + m)                   # create a new array to hold all elements\n        left = 0\n        right = 0\n        index = 0\n\n        # merge both arrays into arr3\n        while left &lt; n and right &lt; m:\n            if arr1[left] &lt;= arr2[right]:\n                arr3[index] = arr1[left]\n                left += 1\n                index += 1\n            else:\n                arr3[index] = arr2[right]\n                right += 1\n                index += 1\n\n        # copy remaining elements from arr1\n        while left &lt; n:\n            arr3[index] = arr1[left]\n            left += 1\n            index += 1\n\n        # copy remaining elements from arr2\n        while right &lt; m:\n            arr3[index] = arr2[right]\n            right += 1\n            index += 1\n\n        # copy back to arr1 and arr2\n        for i in range(n + m):\n            if i &lt; n:\n                arr1[i] = arr3[i]\n            else:\n                arr2[i - n] = arr3[i]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> typing <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">mergeArrays<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr2<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n, m = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr1), <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arr3 = [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] * (n + m)                   <\/span><span style=\"color: #6A9955\"># create a new array to hold all elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        index = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># merge both arrays into arr3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left &lt; n <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> right &lt; m:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr1[left] &lt;= arr2[right]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr3[index] = arr1[left]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                left += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                index += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr3[index] = arr2[right]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                right += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                index += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># copy remaining elements from arr1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left &lt; n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr3[index] = arr1[left]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            left += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            index += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># copy remaining elements from arr2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> right &lt; m:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            arr3[index] = arr2[right]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            right += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            index += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># copy back to arr1 and arr2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n + m):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> i &lt; n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr1[i] = arr3[i]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr2[i - n] = arr3[i]<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create a new array\u00a0<code>arr3<\/code>\u00a0to hold all elements.<\/li>\n\n\n\n<li>We merge both arrays into\u00a0<code>arr3<\/code>\u00a0using two pointers.<\/li>\n\n\n\n<li>After merging, we copy the first\u00a0<code>n<\/code>\u00a0elements back to\u00a0<code>arr1<\/code>\u00a0and the rest to\u00a0<code>arr2<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr1 = [1]<\/code><br><code>arr2 =<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Merge into\u00a0<code>arr3<\/code>:\u00a0<code>[1]<\/code><\/li>\n\n\n\n<li>Copy back:\n<ul class=\"wp-block-list\">\n<li><code>arr1 = [1]<\/code><\/li>\n\n\n\n<li><code>arr2 =<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n + m)<br>We traverse both arrays once.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(n + m)<br>We use extra space for the merged array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple but does not meet the requirement of &#8220;no extra space.&#8221; It\u2019s good for understanding the merging process.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-optimal-solution\">Optimal Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Now, let\u2019s solve the problem&nbsp;<strong>without using extra space<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Swap and Sort:<\/strong><br>Start from the end of\u00a0<code>arr1<\/code>\u00a0and the start of\u00a0<code>arr2<\/code>. If an element in\u00a0<code>arr1<\/code>\u00a0is greater than an element in\u00a0<code>arr2<\/code>, swap them.<\/li>\n\n\n\n<li><strong>Sort Both Arrays:<\/strong><br>After swapping, sort both arrays to make sure all elements are in order.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>By always moving the largest elements to the end and smallest to the beginning, we ensure both arrays are sorted after the final sort.<\/li>\n<\/ol>\n\n\n\n<p>This approach is efficient and meets the problem\u2019s requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def mergeArrays(self, arr1, arr2):\n        n, m = len(arr1), len(arr2)\n        left = n - 1                             # pointer at end of arr1\n        right = 0                               # pointer at start of arr2\n\n        # swap elements if arr1[left] &gt; arr2[right]\n        while left &gt;= 0 and right &lt; m:\n            if arr1[left] &gt; arr2[right]:\n                arr1[left], arr2[right] = arr2[right], arr1[left]\n                left -= 1\n                right += 1\n            else:\n                break\n\n        arr1.sort()                             # sort arr1\n        arr2.sort()                             # sort arr2\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">mergeArrays<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr2<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n, m = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr1), <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        left = n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">                             <\/span><span style=\"color: #6A9955\"># pointer at end of arr1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        right = <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">                               <\/span><span style=\"color: #6A9955\"># pointer at start of arr2<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># swap elements if arr1[left] &gt; arr2[right]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> left &gt;= <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> right &lt; m:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr1[left] &gt; arr2[right]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                arr1[left], arr2[right] = arr2[right], arr1[left]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                left -= <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                right += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arr1.sort()                             <\/span><span style=\"color: #6A9955\"># sort arr1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        arr2.sort()                             <\/span><span style=\"color: #6A9955\"># sort arr2<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use two pointers: one at the end of\u00a0<code>arr1<\/code>\u00a0and one at the start of\u00a0<code>arr2<\/code>.<\/li>\n\n\n\n<li>If\u00a0<code>arr1[left]<\/code>\u00a0is greater than\u00a0<code>arr2[right]<\/code>, we swap them and move the pointers.<\/li>\n\n\n\n<li>After all necessary swaps, we sort both arrays.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>arr1 = [1]<\/code><br><code>arr2 =<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare and swap:\n<ul class=\"wp-block-list\">\n<li>Swap 7 and 0 \u2192\u00a0<code>arr1 = [1]<\/code>,\u00a0<code>arr2 =<\/code><\/li>\n\n\n\n<li>Swap 5 and 2 \u2192\u00a0<code>arr1 = [1]<\/code>,\u00a0<code>arr2 =<\/code><\/li>\n\n\n\n<li>Swap 3 and 7 \u2192 no swap needed, break<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Sort both arrays:\n<ul class=\"wp-block-list\">\n<li><code>arr1 = [1]<\/code><\/li>\n\n\n\n<li><code>arr2 =<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O((n + m) log(n + m))<br>Due to the final sort of both arrays.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>No extra space used.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and meets the &#8220;no extra space&#8221; requirement. It\u2019s the best approach for interviews and large datasets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Merge Without Extra Space&#8221; problem is a great way to learn about in-place array manipulation. Start with brute force to understand the basics, then use the optimal solution for best performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to master array manipulation, &#8220;Merge Without Extra Space&#8221; is an essential coding problem! In this blog, we\u2019ll explain the problem, show you both brute force and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations. Given two sorted arrays&nbsp;a[]&nbsp;and&nbsp;b[]&nbsp;of size&nbsp;n&nbsp;and&nbsp;m&nbsp;respectively, the task is to merge<\/p>\n","protected":false},"author":1,"featured_media":527,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[7,18],"class_list":{"0":"post-525","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-array","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/merge-without-extra-space-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=525"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/525\/revisions\/528"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/527"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}