{"id":617,"date":"2025-07-10T13:44:44","date_gmt":"2025-07-10T08:14:44","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=617"},"modified":"2026-04-04T14:33:41","modified_gmt":"2026-04-04T09:03:41","slug":"median-of-two-sorted-arrays","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/","title":{"rendered":"Median of Two Sorted Arrays | Leetcode 4 | Optimal Binary Search Solution"},"content":{"rendered":"\n<p>The &#8220;Median of Two Sorted Arrays&#8221; problem is a classic and tricky coding interview question. It tests your understanding of merging, searching, and partitioning arrays. <\/p>\n\n\n\n<p>In this blog, we\u2019ll explain the problem, walk you through brute force, better, and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/median-of-two-sorted-arrays\/description\/\" 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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement-\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>Given two sorted arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;of size&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>n<\/code>&nbsp;respectively, return&nbsp;<strong>the median<\/strong>&nbsp;of the two sorted arrays.<\/p>\n\n\n\n<p>The overall run time complexity should be&nbsp;<code>O(log (m+n))<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums1 = [1,3], nums2 = [2]<br><strong>Output:<\/strong> 2.00000<br><strong>Explanation:<\/strong> merged array = [1,2,3] and median is 2.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums1 = [1,2], nums2 = [3,4]<br><strong>Output:<\/strong> 2.50000<br><strong>Explanation:<\/strong> merged array = [1,2,3,4] and median is (2 + 3) \/ 2 = 2.5.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>nums1.length == m<\/code><\/li>\n\n\n\n<li><code>nums2.length == n<\/code><\/li>\n\n\n\n<li><code>0 &lt;= m &lt;= 1000<\/code><\/li>\n\n\n\n<li><code>0 &lt;= n &lt;= 1000<\/code><\/li>\n\n\n\n<li><code>1 &lt;= m + n &lt;= 2000<\/code><\/li>\n\n\n\n<li><code>-10<sup>6<\/sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6<\/sup><\/code><\/li>\n<\/ul>\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-be1dd3a9-d51d-4819-8652-324d034cba36\" 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\/median-of-two-sorted-arrays\/#0-problem-statement-\" style=\"\">Problem Statement<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#1-brute-force-solution-merge-and-find-median\" style=\"\">Brute Force Solution (Merge and Find Median)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#2-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#3-code-implementation\" style=\"\">Code Implementation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#4-better-solution-find-median-while-merging\" style=\"\">Better Solution (Find Median While Merging)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#5-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#6-code-implementation\" style=\"\">Code Implementation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#7-optimal-solution-binary-search-partition\" style=\"\">Optimal Solution (Binary Search Partition)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#8-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#9-code-implementation\" style=\"\">Code Implementation<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/median-of-two-sorted-arrays\/#10-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=\"1-brute-force-solution-merge-and-find-median\">Brute Force Solution (Merge and Find Median)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>How does this solution work?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Merging Two Sorted Arrays:<\/strong><br>Since both arrays are already sorted, we can use the two-pointer technique to merge them into a single sorted array. This is similar to the merge step in merge sort. We keep comparing the smallest elements from both arrays and add the smaller one to our result array.<\/li>\n\n\n\n<li><strong>Finding the Median:<\/strong><br>Once we have the merged array, finding the median is easy:\n<ul class=\"wp-block-list\">\n<li>If the total number of elements is odd, the median is the middle element.<\/li>\n\n\n\n<li>If the total number is even, the median is the average of the two middle elements.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Why does this work?<\/strong><br>By merging, we get a single sorted array, and finding the median in a sorted array is straightforward.<\/p>\n\n\n\n<p><strong>When should you use this?<\/strong><br>This approach is good for small arrays or when you want to build your understanding of the problem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-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:#e1e4e8;--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:#24292e\"><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\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class Solution:\n    def findMedianSortedArrays(self, nums1: List&#91;int&#93;, nums2: List&#91;int&#93;) -> float:\n        result = []\n        n, m = len(nums1), len(nums2)\n        i, j = 0, 0\n        while i &lt; n and j &lt; m:\n            if nums1&#91;i&#93; &lt;= nums2&#91;j&#93;:\n                result.append(nums1&#91;i&#93;)\n                i += 1\n            else:\n                result.append(nums2&#91;j&#93;)\n                j += 1\n\n        while i &lt; n:\n            result.append(nums1&#91;i&#93;)\n            i += 1\n        while j &lt; m:\n            result.append(nums2&#91;j&#93;)\n            j += 1\n        l = len(result)\n        if l % 2 == 0:\n            return (result&#91;l \/\/ 2 - 1&#93; + result&#91;l \/\/ 2&#93;) \/ 2\n        return result&#91;l \/\/ 2&#93;<\/textarea><\/pre><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 github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findMedianSortedArrays<\/span><span style=\"color: #E1E4E8\">(self, nums1: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;, nums2: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;) -&gt; <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        result <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n, m <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums1), <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        i, j <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> j <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> m:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93; <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                result.append(nums1&#91;i&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                i <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                result.append(nums2&#91;j&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                j <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            result.append(nums1&#91;i&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            i <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> j <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> m:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            result.append(nums2&#91;j&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            j <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        l <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(result)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> l <\/span><span style=\"color: #F97583\">%<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> (result&#91;l <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">&#93; <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> result&#91;l <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">&#93;) <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> result&#91;l <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">&#93;<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-better-solution-find-median-while-merging\">Better Solution (Find Median While Merging)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>How does this solution work?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Avoid Full Merge:<\/strong><br>Instead of merging the entire arrays, we only need to find the elements at the median positions. This means we can stop merging as soon as we reach the median.<\/li>\n\n\n\n<li><strong>Two Pointers, Track Only What We Need:<\/strong><br>We use two pointers to go through both arrays. We keep a count of how many elements we&#8217;ve seen so far. When we reach the positions of the median (middle elements), we save their values.<\/li>\n\n\n\n<li><strong>Median Calculation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>For an odd total number, the median is the middle element.<\/li>\n\n\n\n<li>For an even total number, the median is the average of the two middle elements.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Why does this work?<\/strong><br>You don\u2019t need the whole merged array to find the median\u2014just the elements at the median positions.<\/p>\n\n\n\n<p><strong>When should you use this?<\/strong><br>This approach is more efficient in terms of space and slightly faster since it stops early.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-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:#e1e4e8;--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:#24292e\"><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\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class Solution:\n    def findMedianSortedArrays(self, nums1: List&#91;int&#93;, nums2: List&#91;int&#93;) -> float:\n        n1, n2 = len(nums1), len(nums2)\n        n = n1 + n2\n        ind2 = n \/\/ 2\n        ind1 = ind2 - 1\n        cnt = 0\n        ind1el, ind2el = -1, -1\n\n        i, j = 0, 0\n        while i &lt; n1 and j &lt; n2:\n            if nums1&#91;i&#93; &lt; nums2&#91;j&#93;:\n                if cnt == ind1:\n                    ind1el = nums1&#91;i&#93;\n                if cnt == ind2:\n                    ind2el = nums1&#91;i&#93;\n                cnt += 1\n                i += 1\n            else:\n                if cnt == ind1:\n                    ind1el = nums2&#91;j&#93;\n                if cnt == ind2:\n                    ind2el = nums2&#91;j&#93;\n                cnt += 1\n                j += 1\n\n        while i &lt; n1:\n            if cnt == ind1:\n                ind1el = nums1&#91;i&#93;\n            if cnt == ind2:\n                ind2el = nums1&#91;i&#93;\n            cnt += 1\n            i += 1\n\n        while j &lt; n2:\n            if cnt == ind1:\n                ind1el = nums2&#91;j&#93;\n            if cnt == ind2:\n                ind2el = nums2&#91;j&#93;\n            cnt += 1\n            j += 1\n\n        if n % 2 == 1:\n            return float(ind2el)\n        return (ind1el + ind2el) \/ 2.0<\/textarea><\/pre><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 github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findMedianSortedArrays<\/span><span style=\"color: #E1E4E8\">(self, nums1: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;, nums2: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;) -&gt; <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n1, n2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums1), <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> n1 <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> n2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ind2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ind1 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> ind2 <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        cnt <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        ind1el, ind2el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        i, j <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n1 <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> j <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93; <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    ind1el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    ind2el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                cnt <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                i <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    ind1el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    ind2el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                cnt <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                j <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> i <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ind1el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ind2el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums1&#91;i&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            cnt <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            i <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> j <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> n2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ind1el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> cnt <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> ind2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                ind2el <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> nums2&#91;j&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            cnt <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            j <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">%<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(ind2el)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> (ind1el <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> ind2el) <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2.0<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\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-binary-search-partition\">Optimal Solution (Binary Search Partition)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>How does this solution work?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Key Idea \u2013 Partition the Arrays:<\/strong><br>The goal is to partition the two arrays into two halves such that:\n<ul class=\"wp-block-list\">\n<li>The left half contains the same number of elements as the right half (or one more if the total is odd).<\/li>\n\n\n\n<li>Every element in the left half is less than or equal to every element in the right half.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why Partition?<\/strong><br>If you can split the arrays this way, the median will be either:\n<ul class=\"wp-block-list\">\n<li>The maximum of the left half (if odd total length).<\/li>\n\n\n\n<li>The average of the maximum of the left half and the minimum of the right half (if even total length).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>How to Find the Partition?<\/strong>\n<ul class=\"wp-block-list\">\n<li>Always binary search the smaller array for efficiency.<\/li>\n\n\n\n<li>For a given position in the first array, you know exactly where the cut should be in the second array so that the left and right halves have the correct number of elements.<\/li>\n\n\n\n<li>Use binary search to find the correct partition where:\n<ul class=\"wp-block-list\">\n<li>The largest value on the left of the partition in the first array is less than or equal to the smallest value on the right of the partition in the second array.<\/li>\n\n\n\n<li>And vice versa.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>This method ensures you always find the correct median in O(log(min(n, m))) time, which is much faster than merging.<\/li>\n<\/ol>\n\n\n\n<p><strong>When should you use this?<\/strong><br>This is the best approach for large arrays or when you want the most efficient solution.<\/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:#e1e4e8;--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:#24292e\"><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\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class Solution:\n    def findMedianSortedArrays(self, nums1: List&#91;int&#93;, nums2: List&#91;int&#93;) -> float:\n        n1, n2 = len(nums1), len(nums2)\n        # Always binary search on the smaller array\n        if n1 > n2:\n            return self.findMedianSortedArrays(nums2, nums1)\n        \n        n = n1 + n2\n        left = (n1 + n2 + 1) \/\/ 2  # Number of elements in left half\n        low, high = 0, n1\n        \n        while low &lt;= high:\n            mid1 = (low + high) \/\/ 2\n            mid2 = left - mid1\n            \n            l1 = float('-inf') if mid1 == 0 else nums1&#91;mid1 - 1&#93;\n            l2 = float('-inf') if mid2 == 0 else nums2&#91;mid2 - 1&#93;\n            r1 = float('inf') if mid1 == n1 else nums1&#91;mid1&#93;\n            r2 = float('inf') if mid2 == n2 else nums2&#91;mid2&#93;\n            \n            # Check if we have a valid partition\n            if l1 &lt;= r2 and l2 &lt;= r1:\n                if n % 2 == 1:\n                    return float(max(l1, l2))\n                else:\n                    return (max(l1, l2) + min(r1, r2)) \/ 2.0\n            elif l1 > r2:\n                high = mid1 - 1\n            else:\n                low = mid1 + 1\n        return 0.0  # Should not reach here<\/textarea><\/pre><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 github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">findMedianSortedArrays<\/span><span style=\"color: #E1E4E8\">(self, nums1: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;, nums2: List&#91;<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">&#93;) -&gt; <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n1, n2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums1), <\/span><span style=\"color: #79B8FF\">len<\/span><span style=\"color: #E1E4E8\">(nums2)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Always binary search on the smaller array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> n1 <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> n2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.findMedianSortedArrays(nums2, nums1)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        n <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> n1 <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> n2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        left <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (n1 <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> n2 <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\">  <\/span><span style=\"color: #6A737D\"># Number of elements in left half<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low, high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">, n1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid1 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> left <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> mid1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            l1 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&#39;-inf&#39;<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> mid1 <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\"> nums1&#91;mid1 <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            l2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&#39;-inf&#39;<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> mid2 <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\"> nums2&#91;mid2 <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            r1 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&#39;inf&#39;<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> mid1 <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> n1 <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\"> nums1&#91;mid1&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            r2 <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&#39;inf&#39;<\/span><span style=\"color: #E1E4E8\">) <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> mid2 <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> n2 <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\"> nums2&#91;mid2&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #6A737D\"># Check if we have a valid partition<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> l1 <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> r2 <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> l2 <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> r1:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> n <\/span><span style=\"color: #F97583\">%<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">float<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(l1, l2))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> (<\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(l1, l2) <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">min<\/span><span style=\"color: #E1E4E8\">(r1, r2)) <\/span><span style=\"color: #F97583\">\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2.0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">elif<\/span><span style=\"color: #E1E4E8\"> l1 <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> r2:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid1 <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid1 <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0.0<\/span><span style=\"color: #E1E4E8\">  <\/span><span style=\"color: #6A737D\"># Should not reach here<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Median of Two Sorted Arrays&#8221; problem is a fantastic example of how you can optimize a solution step by step:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start with brute force<\/strong>&nbsp;to get the basics.<\/li>\n\n\n\n<li><strong>Use a better approach<\/strong>&nbsp;to improve space and time.<\/li>\n\n\n\n<li><strong>Master the optimal binary search partition<\/strong>&nbsp;for the fastest solution.<\/li>\n<\/ul>\n\n\n\n<p>If you understand the intuition behind each method, you\u2019ll be able to tackle similar problems with confidence.<\/p>\n\n\n\n<div class=\"wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Watch our python dsa course on youtube (200+ questions)<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Median of Two Sorted Arrays&#8221; problem is a classic and tricky coding interview question. It tests your understanding of merging, searching, and partitioning arrays. In this blog, we\u2019ll explain the problem, walk you through brute force, better, and optimal solutions, and make everything easy to understand with code comments, dry runs, and clear explanations.<\/p>\n","protected":false},"author":1,"featured_media":618,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[28,18],"class_list":{"0":"post-617","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-binary-search-on-answers","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/median-of-two-sorted-arrays-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\/617","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=617"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":1122,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions\/1122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/618"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}