{"id":88,"date":"2023-07-21T21:05:47","date_gmt":"2023-07-21T20:05:47","guid":{"rendered":"https:\/\/techedges.in\/?page_id=88"},"modified":"2023-07-21T21:05:47","modified_gmt":"2023-07-21T20:05:47","slug":"explanation-of-find-median-of-two-sorted-arrays","status":"publish","type":"page","link":"https:\/\/techedges.in\/index.php\/explanation-of-find-median-of-two-sorted-arrays\/","title":{"rendered":"Explanation of Find Median of Two Sorted Arrays:"},"content":{"rendered":"\n<p>The &#8220;Find Median of Two Sorted Arrays&#8221; problem involves finding the median element of the merged array formed by combining two sorted arrays. The median is the middle element in a sorted array, or the average of the two middle elements if the array has an even length.<\/p>\n\n\n\n<p><strong>Step-by-Step Approach:<\/strong><\/p>\n\n\n\n<ol>\n<li>Calculate the total length of the merged array, <code>totalLength<\/code>, which is the sum of the lengths of both input arrays.<\/li>\n\n\n\n<li>Determine the indices of the two middle elements of the merged array, <code>mid1<\/code> and <code>mid2<\/code>. If <code>totalLength<\/code> is odd, both <code>mid1<\/code> and <code>mid2<\/code> will be the same, representing the median element. If <code>totalLength<\/code> is even, <code>mid1<\/code> and <code>mid2<\/code> will be the middle elements used to calculate the median.<\/li>\n\n\n\n<li>Merge the two sorted arrays while keeping track of the elements up to the indices <code>mid1<\/code> and <code>mid2<\/code> in variables <code>element1<\/code> and <code>element2<\/code>, respectively. This can be done with a two-pointer approach.<\/li>\n\n\n\n<li>If <code>totalLength<\/code> is odd, the median is the maximum of <code>element1<\/code> and <code>element2<\/code>.<\/li>\n\n\n\n<li>If <code>totalLength<\/code> is even, the median is the average of <code>element1<\/code> and <code>element2<\/code>.<\/li>\n<\/ol>\n\n\n\n<p><strong>Java Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FindMedianSortedArrays {\n\n    public static double findMedianSortedArrays(int&#91;] nums1, int&#91;] nums2) {\n        int totalLength = nums1.length + nums2.length;\n        int mid1 = (totalLength - 1) \/ 2;\n        int mid2 = totalLength \/ 2;\n\n        int pointer1 = 0, pointer2 = 0;\n        int element1 = 0, element2 = 0;\n\n        for (int i = 0; i &lt;= mid2; i++) {\n            element1 = element2;\n            if (pointer1 &lt; nums1.length &amp;&amp; (pointer2 &gt;= nums2.length || nums1&#91;pointer1] &lt; nums2&#91;pointer2])) {\n                element2 = nums1&#91;pointer1++];\n            } else {\n                element2 = nums2&#91;pointer2++];\n            }\n        }\n\n        return (totalLength % 2 == 0) ? (element1 + element2) \/ 2.0 : element2;\n    }\n\n    public static void main(String&#91;] args) {\n        int&#91;] nums1 = {1, 3};\n        int&#91;] nums2 = {2};\n        System.out.println(\"Median of the merged array: \" + findMedianSortedArrays(nums1, nums2));\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Python Solution:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def find_median_sorted_arrays(nums1, nums2):\n    total_length = len(nums1) + len(nums2)\n    mid1, mid2 = (total_length - 1) \/\/ 2, total_length \/\/ 2\n\n    pointer1, pointer2 = 0, 0\n    element1, element2 = 0, 0\n\n    for i in range(mid2 + 1):\n        element1 = element2\n        if pointer1 &lt; len(nums1) and (pointer2 &gt;= len(nums2) or nums1&#91;pointer1] &lt; nums2&#91;pointer2]):\n            element2 = nums1&#91;pointer1]\n            pointer1 += 1\n        else:\n            element2 = nums2&#91;pointer2]\n            pointer2 += 1\n\n    return (element1 + element2) \/ 2 if total_length % 2 == 0 else element2\n\nif __name__ == \"__main__\":\n    nums1 = &#91;1, 3]\n    nums2 = &#91;2]\n    print(\"Median of the merged array:\", find_median_sorted_arrays(nums1, nums2))<\/code><\/pre>\n\n\n\n<p><strong>Explanation of Java\/Python Solution:<\/strong><br>The Java and Python solutions both implement the &#8220;Find Median of Two Sorted Arrays&#8221; problem using a two-pointer approach to merge the sorted arrays and find the median.<\/p>\n\n\n\n<p>The <code>findMedianSortedArrays<\/code> function in Java and <code>find_median_sorted_arrays<\/code> function in Python accept two sorted arrays <code>nums1<\/code> and <code>nums2<\/code> as input and return the median of the merged array.<\/p>\n\n\n\n<p>The example arrays <code>nums1 = [1, 3]<\/code> and <code>nums2 = [2]<\/code> demonstrate the calculation of the median for the merged array <code>[1, 2, 3]<\/code>. The expected output for this example is <code>2.0<\/code>, which is the median of the merged array. If there is an odd number of elements in the merged array, the median will be a single element. If there is an even number of elements, the median will be the average of the two middle elements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Find Median of Two Sorted Arrays&#8221; problem involves finding the median element of the merged array formed by combining two sorted arrays. The median is the middle element in a sorted array, or the average of the two middle elements if the array has an even length. Step-by-Step Approach: Java Solution: Python Solution: Explanation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"saved_in_kubio":false,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"aioseo_notices":[],"kubio_ai_page_context":{"short_desc":"","purpose":"general"},"_links":{"self":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/88"}],"collection":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":1,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/88\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/pages\/88\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/techedges.in\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}