{"id":35,"date":"2026-04-17T17:39:04","date_gmt":"2026-04-17T17:39:04","guid":{"rendered":"https:\/\/portfolio-arrow66.wasmer.app\/?p=35"},"modified":"2026-04-17T17:39:04","modified_gmt":"2026-04-17T17:39:04","slug":"88-merge-sorted-array-lc","status":"publish","type":"post","link":"https:\/\/www.arrow66.dev\/index.php\/88-merge-sorted-array-lc\/","title":{"rendered":"88. Merge Sorted Array -LC"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.com\/problems\/merge-sorted-array\/description\">https:\/\/leetcode.com\/problems\/merge-sorted-array\/description<\/a><\/p>\n\n\n\n<p>This is a well written problem with easy to understand  sample cases.even though it is marked easy in LC i initially find it hard to wrap my head around the problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key findings<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Its an array problem<\/li>\n\n\n\n<li>need to have in-place solution <\/li>\n\n\n\n<li>given arrays are sorted <\/li>\n\n\n\n<li>solve it in o(m+n)<\/li>\n<\/ul>\n\n\n\n<p>Since the arrays are sorted two pointer approach is used for solving the problem.<\/p>\n\n\n\n<p>Python3 &#8211;  solution below <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def merge(self, nums1: List&#91;int], m: int, nums2: List&#91;int], n: int) -&gt; None:\n        \"\"\"\n        Do not return anything, modify nums1 in-place instead.\n        \"\"\"\n        i,j,k = m-1,n-1,len(nums1) -1 \n\n        while i &gt;= 0 and j &gt;= 0:\n            if nums1&#91;i] &gt; nums2&#91;j]:\n                nums1&#91;k] = nums1&#91;i]\n                i -=1\n            else:\n                nums1&#91;k] = nums2&#91;j]\n                j -=1\n            k -=1\n        if j &gt;= 0:\n            nums1&#91;:j+1] = nums2&#91;:j+1]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\">Here the two pointers are initiated with the last value of the array because the arrays are already sorted and we want to avoid moving all the value to the right position in the case if we insert the value of nums2 at the beginning of the array instead .<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edge conditions<\/h2>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\n<p>nums1 = [0]<\/p>\n\n\n\n<p>nums2 = [4]<\/p>\n\n\n\n<p>Expected output nums1 = [4]<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\n<p>nums1 = [4, 5, 6, 0, 0, 0]<\/p>\n\n\n\n<p>nums2 = [1, 2, 3] output = [1,2,3,4,5,6]<\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/merge-sorted-array\/description This is a well written problem with easy to understand sample cases.even though it is marked easy in LC i initially find it hard to wrap my head around the problem. Key findings Since the arrays are sorted two pointer approach is used for solving the problem. Python3 &#8211; solution below Here the two [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[5],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-problems","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.arrow66.dev\/index.php\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}