Skip to content

Commit b16e832

Browse files
committed
Detailed Explanation added for better understanding
1 parent b5f4556 commit b16e832

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

dsa-solutions/lc-solutions/0000-0099/0004-Median-of-two-Sorted-Array.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,55 @@ The binary search approach is more efficient and recommended for solving the pro
756756

757757
:::
758758

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:
787+
$$
788+
\text{maxX} \leq \text{minY} \quad \text{and} \quad \text{maxY} \leq \text{minX}
789+
$$
790+
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:
794+
$$
795+
\text{median} = \frac{\text{max(maxX, maxY)} + \text{min(minX, minY)}}{2}
796+
$$
797+
- If the total number of elements is odd, the median is the maximum element of the left partition:
798+
$$
799+
\text{median} = \text{max(maxX, maxY)}
800+
$$
801+
802+
7. **Adjust Binary Search**:
803+
- If $$ \text{maxX} > \text{minY} $$, it means we need to move the partition in $$ \text{nums1} $$ to the left, so adjust $$ \text{high} $$.
804+
- If $$ \text{maxY} > \text{minX} $$, it means we need to move the partition in $$ \text{nums1} $$ to the right, so adjust $$ \text{low} $$.
805+
806+
:::
807+
759808

760809
## References
761810

0 commit comments

Comments
 (0)