{"id":357,"date":"2023-11-11T10:07:51","date_gmt":"2023-11-11T10:07:51","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=357"},"modified":"2023-11-17T06:15:51","modified_gmt":"2023-11-17T06:15:51","slug":"median-of-two-sorted-arrays","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/median-of-two-sorted-arrays\/","title":{"rendered":"Median of Two Sorted Arrays (C++, Java, Python)"},"content":{"rendered":"\n<p>The problem of finding the median of two sorted arrays is a classic algorithmic challenge that has intrigued computer scientists, mathematicians, and programmers for years. In this article, we will solve the leetcode problem of How to Find the Median of Two Sorted Arrays using two different approaches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the Median of a Sorted Array?<\/strong><\/h2>\n\n\n\n<p><strong>The median of an array is referred to as the center element of the sorted array. <\/strong>There can be two cases of finding it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>If the array contains an odd number of elements, the median is the middle element in the sorted array.<\/li>\n\n\n\n<li>If the array has an even number of elements, the median is the average of the two middle elements in the sorted array.<\/li>\n<\/ol>\n\n\n\n<p>Let us take a simple example.<\/p>\n\n\n\n<p>Consider the array {1, 2, 3, 4, 5}.&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-1024x288.png\" alt=\"Median of a sorted array\n\" class=\"wp-image-358\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m1.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Since the array is of odd length, the median of the array is simply the middle element that is, 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Naive Approach<\/strong><\/h3>\n\n\n\n<p>Before delving into the optimized approach, let&#8217;s briefly discuss the naive approach and its limitations. <strong>The naive approach involves merging the two sorted arrays into a single array and then finding the median of the merged array.\u00a0<\/strong><\/p>\n\n\n\n<p>Since the arrays are already sorted, we will simply maintain two pointers at the initial of both arrays move them accordingly, and add them into the new merged array.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will maintain two pointers and keep them at the 0th index of both arrays.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-1024x288.png\" alt=\"0th index of both array\" class=\"wp-image-359\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m2.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>We will then compare the integers add the smaller integer into the merged array and move the pointer one index ahead<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"400\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-1024x400.png\" alt=\"add the smaller integer into the merged array\" class=\"wp-image-360\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-1024x400.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-300x117.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-768x300.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-750x293.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3-1140x445.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m3.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Similarly, we will traverse both arrays and make the merged array.<\/li>\n\n\n\n<li>Once the merged array is formed we will find the median of the array as discussed above.<\/li>\n\n\n\n<li>Therefore, the median = 4+5\/2 = 4<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-1024x288.png\" alt=\"Merged array and its median\" class=\"wp-image-361\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m4.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>This approach has a time complexity of O(n+k) as we need to traverse both arrays to merge them and then find the median. Since we are taking an extra array of size n+k, the space complexity is O(n+k).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Optimized Approach<\/strong><\/h3>\n\n\n\n<p>The optimized approach is based on the concept of <a href=\"https:\/\/favtutor.com\/blogs\/divide-and-conquer-algorithm\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blogs\/divide-and-conquer-algorithm\" rel=\"noreferrer noopener\">divide and conquer<\/a>, similar to binary search. <strong>The main idea is to divide the arrays into two parts and compare the medians of both arrays.<\/strong> Based on the comparison, we can discard certain elements and narrow down the search space until we find the median.<\/p>\n\n\n\n<p>Let&#8217;s get into the depth with an example:<\/p>\n\n\n\n<p>Array1 = {1, 3, 7, 8}<\/p>\n\n\n\n<p>Array2 = {2, 4, 6}<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-1024x288.png\" alt=\"Example of optimized approach\" class=\"wp-image-362\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m5.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>Given the two arrays, let us try to find our answer.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The combined length of the arrays is <strong>3+4 = 7.<\/strong><\/li>\n\n\n\n<li>Therefore, we know that our median will be at the 7\/2 = 4th position of the merged array.<\/li>\n\n\n\n<li>Let us try not to use extra space and apply the divide and conquer algorithm.<\/li>\n\n\n\n<li>We will try to form symmetric spaces keeping <strong>3 <\/strong>elements in left space and <strong>4 <\/strong>elements in the right space.<\/li>\n\n\n\n<li>Since the length of the second is 3, we can determine that there must be at least 1 element of the array 1 in the left space.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-1024x288.png\" alt=\"Case 1: 4th position\" class=\"wp-image-363\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m6.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li>Thus, considering array 1, it can have between 0 to 6 elements.<\/li>\n\n\n\n<li>We will thus apply a Binary search to figure out how many elements to select from array 1.<\/li>\n\n\n\n<li>Low = 0, High = 3. So, mid = 2.<\/li>\n\n\n\n<li>Now we will update the mid as follows:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If L1&gt;R1, high = mid &#8211; 1.<\/li>\n\n\n\n<li>If L2&gt;R2, low = mid+1.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-1024x288.png\" alt=\"Case 2: Mid = 2\" class=\"wp-image-365\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-1024x288.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-300x84.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-768x216.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-750x211.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7-1140x321.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m7.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ol class=\"wp-block-list\" start=\"10\">\n<li>Here, we can see that both L1 and L2 are lesser than R1 and R2. Therefore, the left and right segments are sorted.<\/li>\n\n\n\n<li>Now, we can find the median at min(R1, R2)<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"400\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-1024x400.png\" alt=\"Median of array: 4\" class=\"wp-image-366\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-1024x400.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-300x117.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-768x300.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-750x293.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8-1140x445.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/m8.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Code<\/strong><\/h3>\n\n\n\n<p>Here is the C++ program to find median of two sorted arrays using an optimized approach:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;C++&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\ndouble findMedianSortedArrays(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {\n    \/\/ Ensure nums1 is the smaller array\n    if (nums1.size() &gt; nums2.size()) {\n        swap(nums1, nums2);\n    }\n\n    int m = nums1.size();\n    int n = nums2.size();\n    int left = 0, right = m, partitionX, partitionY;\n    \n    while (left &lt;= right) {\n        \/\/ Choose a partition index in the smaller array nums1\n        partitionX = (left + right) \/ 2;\n        \/\/ Calculate the corresponding partition index in nums2\n        partitionY = (m + n + 1) \/ 2 - partitionX;\n        \n        \/\/ Calculate maximum and minimum values in the left and right partitions\n        int maxX = (partitionX == 0) ? INT_MIN : nums1[partitionX - 1];\n        int maxY = (partitionY == 0) ? INT_MIN : nums2[partitionY - 1];\n        int minX = (partitionX == m) ? INT_MAX : nums1[partitionX];\n        int minY = (partitionY == n) ? INT_MAX : nums2[partitionY];\n        \n        \/\/ Check if the partitions are correct\n        if (maxX &lt;= minY &amp;&amp; maxY &lt;= minX) {\n            \/\/ Calculate the median\n            if ((m + n) % 2 == 0) {\n                return (max(maxX, maxY) + min(minX, minY)) \/ 2.0;\n            } \n          \telse {\n                return max(maxX, maxY);\n            }\n        }\n      \telse if (maxX &gt; minY) {\n            \/\/ Adjust the partition indexes\n            right = partitionX - 1;\n        } \n      \telse {\n            left = partitionX + 1;\n        }\n    }\n    \n    return 0.0;  \/\/ Return 0.0 if no median found\n}\n\nint main() {\n    vector&lt;int&gt; nums1 = {1, 3, 7, 8};\n    vector&lt;int&gt; nums2 = {2, 4, 6};\n    double median = findMedianSortedArrays(nums1, nums2);\n    cout &lt;&lt; &quot;Median: &quot; &lt;&lt; median &lt;&lt; endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Code<\/strong><\/h3>\n\n\n\n<p>Below is the Python program using an optimized approach:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def findMedianSortedArrays(nums1, nums2):\n    # Ensure nums1 is the smaller array\n    if len(nums1) &gt; len(nums2):\n        nums1, nums2 = nums2, nums1\n\n    m, n = len(nums1), len(nums2)\n    left, right, partitionX, partitionY = 0, m, 0, 0\n\n    while left &lt;= right:\n        # Choose a partition index in the smaller array nums1\n        partitionX = (left + right) \/\/ 2\n        # Calculate the corresponding partition index in nums2\n        partitionY = (m + n + 1) \/\/ 2 - partitionX\n\n        # Calculate maximum and minimum values in the left and right partitions\n        maxX = float('-inf') if partitionX == 0 else nums1[partitionX - 1]\n        maxY = float('-inf') if partitionY == 0 else nums2[partitionY - 1]\n        minX = float('inf') if partitionX == m else nums1[partitionX]\n        minY = float('inf') if partitionY == n else nums2[partitionY]\n\n        # Check if the partitions are correct\n        if maxX &lt;= minY and maxY &lt;= minX:\n            # Calculate the median\n            if (m + n) % 2 == 0:\n                return (max(maxX, maxY) + min(minX, minY)) \/ 2.0\n            else:\n                return float(max(maxX, maxY))\n        elif maxX &gt; minY:\n            # Adjust the partition indexes\n            right = partitionX - 1\n        else:\n            left = partitionX + 1\n\n    return 0.0  # Return 0.0 if no median found\n\nnums1 = [1, 3, 7, 8]\nnums2 = [2, 4, 6]\nmedian = findMedianSortedArrays(nums1, nums2)\nprint(&quot;Median:&quot;, median)<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java Code<\/strong><\/h3>\n\n\n\n<p>Check the Java program below to find the median of two sorted arrays:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Java&quot;,&quot;modeName&quot;:&quot;java&quot;}\">public class MedianOfTwoSortedArrays {\n    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {\n        \/\/ Ensure nums1 is the smaller array\n        if (nums1.length &gt; nums2.length) {\n            int[] temp = nums1;\n            nums1 = nums2;\n            nums2 = temp;\n        }\n\n        int m = nums1.length;\n        int n = nums2.length;\n        int left = 0;\n        int right = m;\n        int partitionX, partitionY;\n\n        while (left &lt;= right) {\n            \/\/ Choose a partition index in the smaller array nums1\n            partitionX = (left + right) \/ 2;\n            \/\/ Calculate the corresponding partition index in nums2\n            partitionY = (m + n + 1) \/ 2 - partitionX;\n\n            \/\/ Calculate maximum and minimum values in the left and right partitions\n            int maxX = (partitionX == 0) ? Integer.MIN_VALUE : nums1[partitionX - 1];\n            int maxY = (partitionY == 0) ? Integer.MIN_VALUE : nums2[partitionY - 1];\n            int minX = (partitionX == m) ? Integer.MAX_VALUE : nums1[partitionX];\n            int minY = (partitionY == n) ? Integer.MAX_VALUE : nums2[partitionY];\n\n            \/\/ Check if the partitions are correct\n            if (maxX &lt;= minY &amp;&amp; maxY &lt;= minX) {\n                \/\/ Calculate the median\n                if ((m + n) % 2 == 0) {\n                    return (double) (Math.max(maxX, maxY) + Math.min(minX, minY)) \/ 2.0;\n                } \n              \telse {\n                    return (double) Math.max(maxX, maxY);\n                }\n            } \n          \telse if (maxX &gt; minY) {\n                \/\/ Adjust the partition indexes\n                right = partitionX - 1;\n            } \n          \telse {\n                left = partitionX + 1;\n            }\n        }\n\n        return 0.0;  \/\/ Return 0.0 if no median found\n    }\n\n    public static void main(String[] args) {\n        int[] nums1 = {1, 3, 7, 8};\n        int[] nums2 = {2, 4, 6};\n        double median = findMedianSortedArrays(nums1, nums2);\n        System.out.println(&quot;Median: &quot; + median);\n    }\n}<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedbca\"><code>Median: 4.0<\/code><\/pre>\n\n\n\n<p>The time complexity of this logarithmic approach is O(log(min(n, k))), where n and k are the sizes of the two arrays. Since we are dividing the search space in half at each step, the algorithm converges to the median in logarithmic time.<\/p>\n\n\n\n<p>The space complexity of this algorithm is O(1), as it only requires a constant amount of extra space to store the variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Analysis of Both Approaches<\/strong><\/h3>\n\n\n\n<p>Let\u2019s see a quick review of all the approaches.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Approach<\/strong><\/td><td><strong>Time Complexity<\/strong><\/td><td><strong>Space Complexity<\/strong><\/td><td><strong>Details<\/strong><\/td><\/tr><tr><td>Naive Approach<\/td><td>O(n+k)<\/td><td>O(n+k)<\/td><td>It involves merging the two sorted arrays into a single array and finding the median of the merged array.&nbsp;<\/td><\/tr><tr><td>Divide and Conquer Approach<\/td><td>O(log(n+k))<\/td><td>O(1)<\/td><td>This approach involves dividing the arrays into two parts and comparing the medians of both arrays based on divide and conquer strategy.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we learned how to find the median of a sorted array. By leveraging the divide and conquer strategy, we were able to efficiently narrow down the search space and find it in O(log(n+k)) time. Solving this problem efficiently is not only a demonstration of algorithmic processes but also has practical applications in various domains, including data analysis, numerical computing, and more. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s solve the leetcode problem of how to find the median of two sorted arrays using two different approaches along with code in C++, Java, and Python.<\/p>\n","protected":false},"author":10,"featured_media":369,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[7],"tags":[27,30,14,15],"class_list":["post-357","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures-algorithms","tag-binary-search","tag-divide-conquer","tag-dsa","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=357"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/357\/revisions\/486"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/369"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}