# 4.Median of Two Sorted Arrays - Hard

### Problem

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

**Example 2:**

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

### Thoughts <a href="#thoughts" id="thoughts"></a>

1. Define the Solution class with a single findMedianSortedArrays candidate method.&#x20;
2. Inside the findMedianSortedArrays () method, first merge two array parameters into a single sorted array using the sorted () method and assign it to the new\_list variable.&#x20;
3. Then get the length of the merged array, and assign it to the length variable.&#x20;
4. Check if the length of the merged array is an even number using an if statement.&#x20;
5. If it is even, calculate the median by taking an average of the two middle elements of the merged list and assigning it to the median variable.&#x20;
6. &#x20;If the length of an array is odd, take the middle element of the merged list and assign it to the median variable.&#x20;
7. Finally, return the median as the float data type.&#x20;
8. Outside the class, define the main() method and call the findMedianSortedArrays() method for two test cases.&#x20;
9. Run the program and get the expected result.

### Solution <a href="#thoughts" id="thoughts"></a>

Time Complexity: O(n log n). The sorted() method takes in O(n log n) time for sorting.\
Space Complexity: O(n). A new list new\_list is created when we merger the two lists and this requires O(n) space.

```
class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        # merge two lists into one
        new_list = sorted(nums1 + nums2)

        length = len(new_list) 
        # get the median
        if(length % 2 == 0): 
            # take the average of the two elements
            median = (new_list[int(length / 2)] + new_list[int(length / 2) - 1]) / 2
        else: 
            # get the middle element
            median = new_list[int(length/2)] 
          
        return median
        
def main():
    solution = Solution()
    
    # Test Cases
    assert solution.findMedianSortedArrays([1,3], [2]) == 2.00000
    assert solution.findMedianSortedArrays([1,2], [3,4]) == 2.50000
    
if __name__ == "__main__":
    main()

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helfipangestu.gitbook.io/leetcode-solutions/solution-1-50/4.median-of-two-sorted-arrays-hard.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
