Skip to content

Update Median of Two Sorted Arrays(Leetcode) #358

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

Merged
merged 2 commits into from
Jun 2, 2024
Merged

Update Median of Two Sorted Arrays(Leetcode) #358

merged 2 commits into from
Jun 2, 2024

Conversation

ajay-dhangar
Copy link
Member

@ajay-dhangar ajay-dhangar commented Jun 2, 2024

When asked to find the median of two sorted arrays, a direct approach that merges the two arrays and then finds the median will work but isn't optimal. Given the problem's constraints, we can leverage the fact that the arrays are already sorted and use binary search to find the median in $O(\log(\min(n, m)))$ time complexity.

The key idea is to use binary search to partition the smaller array in such a way that we can easily find the median by comparing elements around the partition.

Detailed Explanation

  1. Ensure the Smaller Array is First:

    • This step is to make sure we always perform the binary search on the smaller array, which helps us manage the partition logic more easily. Let $\text{nums1}$ be the smaller array and $\text{nums2}$ be the larger array.
  2. Set Up Binary Search:

    • Initialize $\text{low}$ and $\text{high}$ pointers for the binary search on $\text{nums1}$.
    • We aim to partition $\text{nums1}$ and $\text{nums2}$ such that the left side of the combined arrays contains half of the elements, and the right side contains the other half.
  3. Partitioning the Arrays:

    • Calculate $\text{partitionX}$ as the midpoint of $\text{nums1}$.
    • Calculate $\text{partitionY}$ such that the left side of the combined arrays has the same number of elements as the right side. This can be achieved by:

    $$\text{partitionY} = \frac{(x + y + 1)}{2} - \text{partitionX}$$

    where $x$ and $y$ are the lengths of $\text{nums1}$ and $\text{nums2}$ respectively.

  4. Boundary Conditions:

    • Handle cases where partitions might go out of bounds. If $\text{partitionX}$ is 0, it means there are no elements on the left side of $\text{nums1}$. If $\text{partitionX}$ is $x$, it means there are no elements on the right side of $\text{nums1}$.
  5. Check Valid Partition:

    • A valid partition is one where the maximum element on the left side of both partitions is less than or equal to the minimum element on the right side of both partitions:

      $$\text{maxX} \leq \text{minY} \quad \text{and} \quad \text{maxY} \leq \text{minX}$$

    Here, $\text{maxX}$ is the largest element on the left side of $\text{nums1}$, $\text{minX}$ is the smallest element on the right side of $\text{nums1}$, and similarly for $\text{nums2}$.

  6. Calculate the Median:

    • If the total number of elements $(x + y)$ is even, the median is the average of the two middle values:
      $$\text{median} = \frac{\text{max(maxX, maxY)} + \text{min(minX, minY)}}{2}$$
    • If the total number of elements is odd, the median is the maximum element of the left partition:
      $$\text{median} = \text{max(maxX, maxY)}$$
  7. Adjust Binary Search:

    • If $\text{maxX} > \text{minY}$, it means we need to move the partition in $\text{nums1}$ to the left, so adjust $\text{high}$.
    • If $\text{maxY} > \text{minX}$, it means we need to move the partition in $\text{nums1}$ to the right, so adjust $\text{low}$.

Copy link

vercel bot commented Jun 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
codeharborhub-github-io ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 2, 2024 11:19am

Copy link
Contributor

deepsource-io bot commented Jun 2, 2024

Here's the code health analysis summary for commits 752e80e..b16e832. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource JavaScript LogoJavaScript✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

Copy link

sonarqubecloud bot commented Jun 2, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@ajay-dhangar ajay-dhangar self-assigned this Jun 2, 2024
@ajay-dhangar ajay-dhangar added documentation Improvements or additions to documentation enhancement New feature or request GSSOC'24 GirlScript Summer of Code | Contributor level3 GirlScript Summer of Code | Contributor's Levels gssoc GirlScript Summer of Code | Contributor labels Jun 2, 2024
@ajay-dhangar ajay-dhangar merged commit b3ce06a into main Jun 2, 2024
10 checks passed
@ajay-dhangar ajay-dhangar removed GSSOC'24 GirlScript Summer of Code | Contributor level3 GirlScript Summer of Code | Contributor's Levels gssoc GirlScript Summer of Code | Contributor labels Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant