You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0004-Median-of-two-Sorted-Array.md
+10-11Lines changed: 10 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ In this page, we will solve the problem [Median of Two Sorted Arrays](https://le
16
16
17
17
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return the median of the two sorted arrays.
18
18
19
-
The overall run time complexity should be <code>$O(log (m+n))$</code>.
19
+
The overall run time complexity should be $O(log (m+n))$.
20
20
21
21
### Example 1
22
22
@@ -429,7 +429,7 @@ Let's test the solution with the sample test cases:
429
429
Input: nums1 = [1, 3], nums2 = [2]
430
430
Output: 2.00000
431
431
Explanation: merged array = [1, 2, 3] and median is 2.
432
-
````
432
+
```
433
433
</TabItem>
434
434
435
435
<TabItemvalue="TestCase2"label="Case 2">
@@ -770,8 +770,7 @@ Let's test the solution with the sample test cases:
770
770
Input: nums1 = [1, 3], nums2 = [2]
771
771
Output: 2.00000
772
772
Explanation: merged array = [1, 2, 3] and median is 2.
773
-
````
774
-
773
+
```
775
774
</TabItem>
776
775
<TabItemvalue="TestCase2"label="Case 2">
777
776
```plaintext
@@ -786,7 +785,7 @@ Explanation: merged array = [1, 2, 3, 4] and median is (2 + 3) / 2 = 2.5.
786
785
787
786
:::info
788
787
789
-
**Note**: The binary search approach is more efficient than the divide and conquer approach as it has a better time complexity of <code>$O(log(min(n, m)))$</code> compared to the divide and conquer approach. However, both approaches provide a solution to the problem of finding the median of two sorted arrays.
788
+
**Note**: The binary search approach is more efficient than the divide and conquer approach as it has a better time complexity of $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.
790
789
791
790
**Which approach is best for you?**
792
791
@@ -795,7 +794,7 @@ The binary search approach is more efficient and recommended for solving the pro
795
794
:::
796
795
797
796
:::tip
798
-
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.
797
+
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.
799
798
800
799
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.
801
800
@@ -823,19 +822,19 @@ The key idea is to use binary search to partition the smaller array in such a wa
823
822
824
823
4.**Boundary Conditions**:
825
824
826
-
- 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} $$.
825
+
- 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} $$.
827
826
828
827
5.**Check Valid Partition**:
829
828
830
829
- 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:
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}$$.
833
+
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}$.
835
834
836
835
6.**Calculate the Median**:
837
836
838
-
- If the total number of elements $$(x + y)$$ is even, the median is the average of the two middle values:
837
+
- If the total number of elements $(x + y)$ is even, the median is the average of the two middle values:
0 commit comments