$O(log (m+n))$
.
-Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.
+### Example 1
-The overall run time complexity should be $O(log (m+n))$.
-
-**Example 1:**
-```
+```plaintext
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
```
-**Example 2:**
-```
+### Example 2
+
+```plaintext
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
```
-### Constraints
-- `nums1.length == m`
-- `nums2.length == n`
-- `0 <= m <= 1000`
-- `0 <= n <= 1000`
-- `1 <= m + n <= 2000`
-- `-10^6 <= nums1[i], nums2[i] <= 10^6`
-
-### Solution
-
-#### Approach 1: Merge Sort $(O(m + n))$
-
-**Algorithm:**
-1. Merge `nums1` and `nums2` into a single sorted array.
-2. Find the median of the merged array:
- - If the total number of elements is odd, return the middle element.
- - If the total number of elements is even, return the average of the two middle elements.
-
-**Python Implementation:**
-```python
-def findMedianSortedArrays(nums1, nums2):
- merged = []
- i = j = 0
- while i < len(nums1) and j < len(nums2):
- if nums1[i] < nums2[j]:
- merged.append(nums1[i])
- i += 1
- else:
- merged.append(nums2[j])
- j += 1
- merged.extend(nums1[i:])
- merged.extend(nums2[j:])
-
- n = len(merged)
- if n % 2 == 1:
- return merged[n // 2]
- else:
- return (merged[n // 2 - 1] + merged[n // 2]) / 2.0
-```
+### Constraints:
+
+- $nums1.length == m$
+- $nums2.length == n$
+- $0 <= m <= 1000$
+- $0 <= n <= 1000$
+- $1 <= m + n <= 2000$
+- $-10^6 <= nums1[i], nums2[i] <= 10^6$
+
+---
-#### Approach 2: Binary Search $(O(log(min(m, n))))$
+## Solution for Median of Two Sorted Arrays
-**Intuition:**
-To achieve $O(log(min(m, n)))$ complexity, use binary search on the smaller array. The goal is to find a partition where the left half of both arrays combined is less than or equal to the right half.
+The **Median of Two Sorted Arrays** is a classic algorithm problem on LeetCode ([Problem 4](https://leetcode.com/problems/median-of-two-sorted-arrays/description/)). It requires finding the median of two sorted arrays. Given two sorted arrays `nums1` and `nums2`, the task is to find the median of the two arrays combined.
+
+Here is an explanation of the intuition and approaches to solve this problem:
+
+### intuition
+
+The median is the middle value of a dataset. For an even number of elements, it is the average of the two middle values. If we have two sorted arrays, the naive approach would involve merging both arrays and then finding the median, but this approach does not take full advantage of the fact that the arrays are already sorted and is not efficient for large arrays.
+
+Instead, we can use a more advanced approach to find the median in $O(log(min(n, m)))$
time, where `n` and `m` are the lengths of the two arrays. This involves using a binary search method. The idea is to partition both arrays into two halves such that the combined left half and right half are both sorted and of equal length (or differing by one).
+
++ Input: nums1 = {"[" + nums1.join(", ") + "]"}, nums2 = {"[" + nums2.join(", ") + "]"} +
++ Output: {result} +
+$O(log(min(n, m)))$
, where `n` and `m` are the lengths of the two arrays. The binary search approach helps in reducing the search space and finding the median efficiently.
+- **Space Complexity**: The space complexity of this approach is $O(1)$
as we are using constant extra space.
+
+### Test Cases
+
+Let's test the solution with the sample test cases:
+
++ Input: nums1 = {"[" + nums1.join(", ") + "]"}, nums2 = {"[" + nums2.join(", ") + "]"} +
++ Output: {result} +
+$O(log(min(n, m)))$
, where `n` and `m` are the lengths of the two arrays. Each recursive call divides the problem into half.
+- **Space Complexity**: The space complexity of this approach is $O(1)$
as we are using constant extra space.
+
+### Test Cases
+
+Let's test the solution with the sample test cases:
+
+$O(log(min(n, m)))$
compared to the divide and conquer approach. However, both approaches provide a solution to the problem of finding the median of two sorted arrays.
+
+**Which approach is best for you?**
+
+The binary search approach is more efficient and recommended for solving the problem of finding the median of two sorted arrays. However, the divide and conquer approach is also a valid solution and can be used if needed.
+
+:::
+
+:::tip
+When asked to find the median of two sorted arrays, a direct approach that merges the two arrays and then finds the median will work but isn't optimal. Given the problem's constraints, we can leverage the fact that the arrays are already sorted and use binary search to find the median in $$ O(\log(\min(n, m))) $$ time complexity.
+
+The key idea is to use binary search to partition the smaller array in such a way that we can easily find the median by comparing elements around the partition.
+
+#### Detailed Explanation
+
+1. **Ensure the Smaller Array is First**:
+ - This step is to make sure we always perform the binary search on the smaller array, which helps us manage the partition logic more easily. Let $$ \text{nums1} $$ be the smaller array and $$ \text{nums2} $$ be the larger array.
+
+2. **Set Up Binary Search**:
+ - Initialize $$ \text{low} $$ and $$ \text{high} $$ pointers for the binary search on $$ \text{nums1} $$.
+ - We aim to partition $$ \text{nums1} $$ and $$ \text{nums2} $$ such that the left side of the combined arrays contains half of the elements, and the right side contains the other half.
+
+3. **Partitioning the Arrays**:
+ - Calculate $$ \text{partitionX} $$ as the midpoint of $$ \text{nums1} $$.
+ - Calculate $$ \text{partitionY} $$ such that the left side of the combined arrays has the same number of elements as the right side. This can be achieved by:
+ $$
+ \text{partitionY} = \frac{(x + y + 1)}{2} - \text{partitionX}
+ $$
+
+ where $$ x $$ and $$ y $$ are the lengths of $$ \text{nums1} $$ and $$ \text{nums2} $$ respectively.
+
+4. **Boundary Conditions**:
+ - Handle cases where partitions might go out of bounds. If $$ \text{partitionX} $$ is 0, it means there are no elements on the left side of $$ \text{nums1} $$. If $$ \text{partitionX} $$ is $$ x $$, it means there are no elements on the right side of $$ \text{nums1} $$.
+
+5. **Check Valid Partition**:
+ - A valid partition is one where the maximum element on the left side of both partitions is less than or equal to the minimum element on the right side of both partitions:
+ $$
+ \text{maxX} \leq \text{minY} \quad \text{and} \quad \text{maxY} \leq \text{minX}
+ $$
+ Here, $$ \text{maxX} $$ is the largest element on the left side of $$ \text{nums1} $$, $$ \text{minX} $$ is the smallest element on the right side of $$ \text{nums1} $$, and similarly for $$ \text{nums2} $$.
+
+6. **Calculate the Median**:
+ - If the total number of elements $$ (x + y) $$ is even, the median is the average of the two middle values:
+ $$
+ \text{median} = \frac{\text{max(maxX, maxY)} + \text{min(minX, minY)}}{2}
+ $$
+ - If the total number of elements is odd, the median is the maximum element of the left partition:
+ $$
+ \text{median} = \text{max(maxX, maxY)}
+ $$
+
+7. **Adjust Binary Search**:
+ - If $$ \text{maxX} > \text{minY} $$, it means we need to move the partition in $$ \text{nums1} $$ to the left, so adjust $$ \text{high} $$.
+ - If $$ \text{maxY} > \text{minX} $$, it means we need to move the partition in $$ \text{nums1} $$ to the right, so adjust $$ \text{low} $$.
+
+:::
-- **Space Complexity:**
- - Approach 1: $O(m + n)$ due to the additional space needed for the merged array.
- - Approach 2: $O(1)$ because it uses only a constant amount of additional space.
-### Summary
+## References
-Approach 1 is straightforward but not optimal in terms of time complexity for large input sizes. Approach 2 leverages binary search to efficiently find the median with logarithmic time complexity, making it suitable for large arrays.
+- [LeetCode Problem](https://leetcode.com/problems/median-of-two-sorted-arrays/description/)
+- [GeeksforGeeks Article](https://www.geeksforgeeks.org/median-of-two-sorted-arrays/)
\ No newline at end of file