> For the complete documentation index, see [llms.txt](https://chiragjain.gitbook.io/neetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chiragjain.gitbook.io/neetcode/4-median-of-two-sorted-arrays.md).

# 4. Median of Two Sorted Arrays

## Hard

***

Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.

The overall run time complexity should be `O(log (m+n))`.

&#x20;

**Example 1:**

<pre><code>Input: nums1 = [1,3], nums2 = [2]
<strong>Output:
</strong> 2.00000
<strong>Explanation:
</strong> merged array = [1,2,3] and median is 2.
</code></pre>

**Example 2:**

<pre><code>Input: nums1 = [1,2], nums2 = [3,4]
<strong>Output:
</strong> 2.50000
<strong>Explanation:
</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
</code></pre>

&#x20;

**Constraints:**

* `nums1.length == m`
* `nums2.length == n`
* `0 <= m <= 1000`
* `0 <= n <= 1000`
* `1 <= m + n <= 2000`
* `-106 <= nums1[i], nums2[i] <= 106`
