Median of Two Sorted Arrays
Detailed guide and Python implementation for the 'Median of Two Sorted Arrays' problem.
1. Concept Overview
The 'Median of Two Sorted Arrays' problem is a key challenge in the Binary Search section.
This implementation focuses on hard-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Median of Two Sorted Arrays.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Median of Two Sorted Arrays carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
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)).
Write a function findMedianSortedArrays(nums1: List[int], nums2: List[int]) -> float.
- •nums1.length == m, nums2.length == n
- •0 <= m <= 1000
- •0 <= n <= 1000
- •1 <= m + n <= 2000
- •-10^6 <= nums1[i], nums2[i] <= 10^6
Examples
nums1 = [1, 3], nums2 = [2]
2.0
Merged array = [1, 2, 3]. The median is 2.0.
nums1 = [1, 2], nums2 = [3, 4]
2.5
Merged array = [1, 2, 3, 4]. The median is (2 + 3) / 2 = 2.5.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def find_median_sorted_arrays_opt(nums1, nums2):
A, B = nums1, nums2
total = len(nums1) + len(nums2)
half = total // 2
if len(B) < len(A): A, B = B, A
l, r = 0, len(A) - 1
while True:
i = (l + r) // 2
j = half - i - 2
Aleft = A[i] if i >= 0 else float("-infinity")
Aright = A[i + 1] if (i + 1) < len(A) else float("infinity")
Bleft = B[j] if j >= 0 else float("-infinity")
Bright = B[j + 1] if (j + 1) < len(B) else float("infinity")
if Aleft <= Bright and Bleft <= Aright:
if total % 2:
return min(Aright, Bright)
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2
elif Aleft > Bright:
r = i - 1
else:
l = i + 1Brute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def find_median_sorted_arrays_brute(nums1, nums2):
merged = sorted(nums1 + nums2)
n = len(merged)
if n % 2 == 1:
return float(merged[n // 2])
return (merged[n // 2 - 1] + merged[n // 2]) / 2.0Algorithm Pattern Checklist
When dealing with Binary Search data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.