Challenges
For this issue, follow first challenge on this thread https://app.gitbook.com/o/h0Ny2Dx4tM7wlLiHltUk/s/wNXdoUkfmcozr29fRrfb/~/changes/453/algorithm/more/pigeonhole-principle/challenge
Line 1: Defines a function named
findMedianSortedArrays
that takes two input parameters:nums1
andnums2
, which represent two sorted arrays.
Lines 2-4: These lines check if
nums1
is larger thannums2
in length. If so, the arrays are swapped. This ensures that we perform the binary search on the smaller array, reducing the overall time complexity.
Line 5:
m
andn
store the lengths ofnums1
andnums2
, respectively.Line 6:
imin
andimax
are initialized as the starting and ending indices for binary search onnums1
.half_len
is the half of the combined length of both arrays, used to calculate the partition points.
Line 7: A
while
loop is used for binary search, which continues as long asimin
is less than or equal toimax
.Line 8:
i
is the partition index fornums1
, calculated as the midpoint ofimin
andimax
.Line 9:
j
is the corresponding partition index fornums2
, ensuring that the total number of elements on both sides of the partition is equal or nearly equal.
Lines 10-12: These lines check if the partition in
nums1
is too far to the left, i.e., the element just to the left of the partition innums2
is greater than the element just to the right of the partition innums1
. In this case,imin
is moved to the right (i + 1
) to increasei
in the next iteration.
Lines 13-15: These lines check if the partition in
nums1
is too far to the right, i.e., the element just to the left of the partition innums1
is greater than the element just to the right of the partition innums2
. In this case,imax
is moved to the left (i - 1
) to decreasei
in the next iteration.
Line 16: If neither of the above conditions are true, it means the partition
i
is correct, and the elements on both sides of the partition satisfy the median condition.Lines 17-19: These lines handle the edge cases where
i
orj
is at the boundary of an array:If
i == 0
, it means all elements on the left side of the partition are fromnums2
, somax_of_left
is set tonums2[j-1]
.If
j == 0
, all elements on the left side are fromnums1
, somax_of_left
is set tonums1[i-1]
.Otherwise,
max_of_left
is the larger of the two elements on the left side of the partition from both arrays.
Lines 20-21: If the total number of elements
(m + n)
is odd, the median is simplymax_of_left
, because the middle element is on the left side of the partition.
Lines 22-24: If the total number of elements
(m + n)
is even, the median is the average of the largest element on the left side (max_of_left
) and the smallest element on the right side (min_of_right
):If
i == m
, all elements on the right side are fromnums2
, somin_of_right
isnums2[j]
.If
j == n
, all elements on the right side are fromnums1
, somin_of_right
isnums1[i]
.Otherwise,
min_of_right
is the smaller of the two elements on the right side of the partition from both arrays.
Line 25: Finally, for the even case, return the average of
max_of_left
andmin_of_right
as the median.
Summary:
The function uses binary search to partition the smaller array, adjusting the partition until the correct median is found.
It handles both odd and even total element cases.
The time complexity of this approach is O(log(min(m,n)))O(\log(\text{min}(m, n)))O(log(min(m,n))), making it efficient for large input sizes.
Last updated