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)).
The median is the middle value when all elements are arranged in sorted order.
- If the total number of elements is odd, the median is the middle element.
- If the total number of elements is even, the median is the average of the two middle elements.
Example 1
Input
m = 2 n = 1 nums1 = [1, 3] nums2 = [2]
Output
2.00000
Explanation
The combined sorted array is:
[1, 2, 3]
The middle element is 2, so the median is 2.
Example 2
Input
m = 2 n = 2 nums1 = [1, 2] nums2 = [3, 4]
Output
2.50000
Explanation
The combined sorted array is:
[1, 2, 3, 4]
The two middle elements are 2 and 3.
(2 + 3) / 2 = 2.5
Constraints
Hints:
Hint 1
You do not need to actually merge the two arrays.
Hint 2
Use binary search on the smaller array.
Choose a partition such that:
all elements on the left <= all elements on the right
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 22, 2026
Last updated
Aug 23, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output