{"id":496,"date":"2024-08-06T02:26:42","date_gmt":"2024-08-06T02:26:42","guid":{"rendered":"https:\/\/premsvmm.com\/?p=496"},"modified":"2024-08-06T02:26:42","modified_gmt":"2024-08-06T02:26:42","slug":"merge-sorted-array","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2024\/08\/06\/merge-sorted-array\/","title":{"rendered":"Merge Sorted Array"},"content":{"rendered":"\n<p>You are given two integer arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>, sorted in&nbsp;<strong>non-decreasing order<\/strong>, and two integers&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>n<\/code>, representing the number of elements in&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;respectively.<\/p>\n\n\n\n<p><strong>Merge<\/strong>&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;into a single array sorted in&nbsp;<strong>non-decreasing order<\/strong>.<\/p>\n\n\n\n<p>The final sorted array should not be returned by the function, but instead be&nbsp;<em>stored inside the array&nbsp;<\/em><code>nums1<\/code>. To accommodate this,&nbsp;<code>nums1<\/code>&nbsp;has a length of&nbsp;<code>m + n<\/code>, where the first&nbsp;<code>m<\/code>&nbsp;elements denote the elements that should be merged, and the last&nbsp;<code>n<\/code>&nbsp;elements are set to&nbsp;<code>0<\/code>&nbsp;and should be ignored.&nbsp;<code>nums2<\/code>&nbsp;has a length of&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><strong>Input:<\/strong> nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 <strong>Output:<\/strong> [1,2,2,3,5,6] <strong>Explanation:<\/strong> The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.<\/p>\n\n\n\n<p><strong>Example 2:<\/strong><strong>Input:<\/strong> nums1 = [1], m = 1, nums2 = [], n = 0 <strong>Output:<\/strong> [1] <strong>Explanation:<\/strong> The arrays we are merging are [1] and []. The result of the merge is [1].<\/p>\n\n\n\n<p><strong>Example 3:<\/strong><strong>Input:<\/strong> nums1 = [0], m = 0, nums2 = [1], n = 1 <strong>Output:<\/strong> [1] <strong>Explanation:<\/strong> The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul>\n<li><code>nums1.length == m + n<\/code><\/li>\n\n\n\n<li><code>nums2.length == n<\/code><\/li>\n\n\n\n<li><code>0 &lt;= m, n &lt;= 200<\/code><\/li>\n\n\n\n<li><code>1 &lt;= m + n &lt;= 200<\/code><\/li>\n\n\n\n<li><code>-10<sup>9<\/sup> &lt;= nums1[i], nums2[j] &lt;= 10<sup>9<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Follow up:\u00a0<\/strong>Can you come up with an algorithm that runs in\u00a0<code>O(m + n)<\/code>\u00a0time?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Java&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.ArrayList;\nimport java.util.Comparator;\n\npublic class MergeSortedArray {\n    public void merge(int[] nums1, int m, int[] nums2, int n) {\n        ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n        for (int i = 0; i &lt; m; i++) {\n            list.add(nums1[i]);\n        }\n\n        for (int j = 0; j &lt; n; j++) {\n            list.add(nums2[j]);\n        }\n        list.sort(Comparator.naturalOrder());\n        int index = 0;\n        for (Integer i : list) {\n            nums1[index++] = i;\n        }\n    }\n\n    public static void main(String[] args) {\n        MergeSortedArray obj = new MergeSortedArray();\n        obj.merge(new int[]{1, 2, 3, 0, 0, 0}, 3, new int[]{2, 5, 6}, 3);\n\n    }\n}\n\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integer arrays&nbsp;nums1&nbsp;and&nbsp;nums2, sorted in&nbsp;non-decreasing order, and two integers&nbsp;m&nbsp;and&nbsp;n, representing the number of elements in&nbsp;nums1&nbsp;and&nbsp;nums2&nbsp;respectively. Merge&nbsp;nums1&nbsp;and&nbsp;nums2&nbsp;into a single array sorted in&nbsp;non-decreasing order. The final sorted array should not be returned by the function, but instead be&nbsp;stored inside the array&nbsp;nums1. To accommodate this,&nbsp;nums1&nbsp;has a length of&nbsp;m + n, where the first&nbsp;m&nbsp;elements denote the &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/premsvmm.com\/index.php\/2024\/08\/06\/merge-sorted-array\/\"> <span class=\"screen-reader-text\">Merge Sorted Array<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[10],"tags":[11],"_links":{"self":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/496"}],"collection":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":1,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":497,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/496\/revisions\/497"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}