4. Median of Two Sorted Arrays #35
Answered
by
mah-shamim
mah-shamim
asked this question in
Q&A
-
Given two sorted arrays The overall run time complexity should be Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Answered by
mah-shamim
Jul 29, 2024
Replies: 1 comment 1 reply
-
To solve this problem, we can follow these steps: Let's implement this solution in PHP: 4. Median of Two Sorted Arrays <?php
// Example usage:
$nums1 = [1, 3];
$nums2 = [2];
echo findMedianSortedArrays($nums1, $nums2) . "\n"; // Output: 2.00000
$nums1 = [1, 2];
$nums2 = [3, 4];
echo findMedianSortedArrays($nums1, $nums2) . "\n"; // Output: 2.50000
$nums1 = [0, 0];
$nums2 = [0, 0];
echo findMedianSortedArrays($nums1, $nums2) . "\n"; // Output: 0.00000
$nums1 = [];
$nums2 = [1];
echo findMedianSortedArrays($nums1, $nums2) . "\n"; // Output: 1.00000
$nums1 = [2];
$nums2 = [];
echo findMedianSortedArrays($nums1, $nums2) . "\n"; // Output: 2.00000
?> Explanation:
This solution ensures that the overall run time complexity is (O(\log(m+n))). |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
basharul-siddike
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To solve this problem, we can follow these steps:
Let's implement this solution in PHP: 4. Median of Two Sorted Arrays
Explanation:
Partitioning the Arrays: