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
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -756,6 +756,55 @@ The binary search approach is more efficient and recommended for solving the pro
756
756
757
757
:::
758
758
759
+
:::tip
760
+
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.
761
+
762
+
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.
763
+
764
+
#### Detailed Explanation
765
+
766
+
1.**Ensure the Smaller Array is First**:
767
+
- 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.
768
+
769
+
2.**Set Up Binary Search**:
770
+
- Initialize $$ \text{low} $$ and $$ \text{high} $$ pointers for the binary search on $$ \text{nums1} $$.
771
+
- 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.
772
+
773
+
3.**Partitioning the Arrays**:
774
+
- Calculate $$ \text{partitionX} $$ as the midpoint of $$ \text{nums1} $$.
775
+
- 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:
776
+
$$
777
+
\text{partitionY} = \frac{(x + y + 1)}{2} - \text{partitionX}
778
+
$$
779
+
780
+
where $$ x $$ and $$ y $$ are the lengths of $$ \text{nums1} $$ and $$ \text{nums2} $$ respectively.
781
+
782
+
4.**Boundary Conditions**:
783
+
- 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} $$.
784
+
785
+
5.**Check Valid Partition**:
786
+
- 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} $$.
791
+
792
+
6.**Calculate the Median**:
793
+
- If the total number of elements $$ (x + y) $$ is even, the median is the average of the two middle values:
0 commit comments