-
-
Notifications
You must be signed in to change notification settings - Fork 50.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
Open
anshmittal2004
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
anshmittal2004:add-median-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
| 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!") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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