Challenges
Last updated
Last updated
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
and nums2
, which represent two sorted arrays.
Lines 2-4: These lines check if nums1
is larger than nums2
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
and n
store the lengths of nums1
and nums2
, respectively.
Line 6: imin
and imax
are initialized as the starting and ending indices for binary search on nums1
. 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 as imin
is less than or equal to imax
.
Line 8: i
is the partition index for nums1
, calculated as the midpoint of imin
and imax
.
Line 9: j
is the corresponding partition index for nums2
, 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 in nums2
is greater than the element just to the right of the partition in nums1
. In this case, imin
is moved to the right (i + 1
) to increase i
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 in nums1
is greater than the element just to the right of the partition in nums2
. In this case, imax
is moved to the left (i - 1
) to decrease i
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
or j
is at the boundary of an array:
If i == 0
, it means all elements on the left side of the partition are from nums2
, so max_of_left
is set to nums2[j-1]
.
If j == 0
, all elements on the left side are from nums1
, so max_of_left
is set to nums1[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 simply max_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 from nums2
, so min_of_right
is nums2[j]
.
If j == n
, all elements on the right side are from nums1
, so min_of_right
is nums1[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
and min_of_right
as the median.
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.