-  
-   Notifications  You must be signed in to change notification settings 
- Fork 49.1k
Add median of two sorted arrays implementation #13717 #13742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """ | ||
| Median of Two Sorted Arrays | ||
|  | ||
| Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. | ||
| Check failure on line 4 in divide_and_conquer/median_of_two_sorted_arrays.py   | ||
|  | ||
| The overall run time complexity should be O(log(m + n)). | ||
|  | ||
| Examples: | ||
| Example 1: | ||
| Input: nums1 = [1, 3], nums2 = [2] | ||
| Output: 2.00000 | ||
| Explanation: merged array = [1, 2, 3] and median is 2. | ||
|  | ||
| Example 2: | ||
| Input: nums1 = [1, 2], nums2 = [3, 4] | ||
| Output: 2.50000 | ||
| Explanation: merged array = [1, 2, 3, 4] and median is (2 + 3) / 2 = 2.5. | ||
|  | ||
| Constraints: | ||
| * nums1.length == m | ||
| * nums2.length == n | ||
| * 0 <= m <= 1000 | ||
| * 0 <= n <= 1000 | ||
| * 1 <= m + n <= 2000 | ||
| * -10^6 <= nums1[i], nums2[i] <= 10^6 | ||
|  | ||
| Implementation: Divide and Conquer (Binary Search on partitions). | ||
| Time Complexity: O(log(min(m, n))) | ||
| Space Complexity: O(1) | ||
| """ | ||
|  | ||
| from typing import List | ||
| Check failure on line 32 in divide_and_conquer/median_of_two_sorted_arrays.py   | ||
|  | ||
|  | ||
| def find_median_sorted_arrays(nums1: List[int], nums2: List[int]) -> float: | ||
| Check failure on line 35 in divide_and_conquer/median_of_two_sorted_arrays.py   | ||
| """ | ||
| Find the median of two sorted arrays using binary search on partitions. | ||
|  | ||
| Args: | ||
| nums1 (List[int]): First sorted array | ||
| nums2 (List[int]): Second sorted array | ||
|  | ||
| Returns: | ||
| float: Median of the two arrays | ||
| """ | ||
| # Ensure nums1 is the smaller array | ||
| if len(nums1) > len(nums2): | ||
| nums1, nums2 = nums2, nums1 | ||
|  | ||
| m, n = len(nums1), len(nums2) | ||
| left, right = 0, m | ||
|  | ||
| # Binary search for the partition in nums1 | ||
| while left <= right: | ||
| i = (left + right) // 2 # Partition in nums1 | ||
| j = (m + n + 1) // 2 - i # Partition in nums2 | ||
|  | ||
| # Edge cases for partitions | ||
| left1 = float('-inf') if i == 0 else nums1[i - 1] | ||
| right1 = float('inf') if i == m else nums1[i] | ||
| left2 = float('-inf') if j == 0 else nums2[j - 1] | ||
| right2 = float('inf') if j == n else nums2[j] | ||
|  | ||
| # Check if this partition is correct | ||
| if left1 <= right2 and left2 <= right1: | ||
| # Correct partition found | ||
| if (m + n) % 2 == 0: | ||
| # Even length: average of max(lefts) and min(rights) | ||
| return (max(left1, left2) + min(right1, right2)) / 2 | ||
| else: | ||
| # Odd length: max of lefts | ||
| return max(left1, left2) | ||
| elif left1 > right2: | ||
| # Move partition left in nums1 | ||
| right = i - 1 | ||
| else: | ||
| # Move partition right in nums1 | ||
| left = i + 1 | ||
|  | ||
| # Should not reach here if inputs are valid | ||
| raise ValueError("Input arrays are not sorted or invalid") | ||
|  | ||
|  | ||
| # Optional: Add simple tests | ||
| if __name__ == "__main__": | ||
| assert abs(find_median_sorted_arrays([1, 3], [2]) - 2.0) < 1e-5 | ||
| assert abs(find_median_sorted_arrays([1, 2], [3, 4]) - 2.5) < 1e-5 | ||
| print("All tests passed!") | ||
| Check failure on line 88 in divide_and_conquer/median_of_two_sorted_arrays.py   | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
divide_and_conquer/median_of_two_sorted_arrays.py, please provide doctest for the functionfind_median_sorted_arrays