Problem
You get two arrays of integers, nums1 and nums2. Each array is in non-decreasing order, so each item is equal to or greater than the item before it. Their lengths are m and n.
Return the median of all the items of the two arrays together. The median is the middle value of a sorted list. If the list has an even number of items, the median is the mean of the two middle values.
Your solution must run in O(log (m + n)) time. The checker accepts an answer within 10^-5 of the correct value.
Example 1
- Input:
- nums1 = [1,4,9], nums2 = [2,6]
- Output:
- 4.0
- Explanation:
- The sorted list of all the items is
[1, 2, 4, 6, 9]. The middle value is 4.
Example 2
- Input:
- nums1 = [3,5], nums2 = [8,12]
- Output:
- 6.5
- Explanation:
- The sorted list is
[3, 5, 8, 12]. The two middle values are 5 and 8, and their mean is 6.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
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Reverse Pairs
Coding · Array · Binary Indexed Tree · Binary Search
- Search a 2D Matrix II
Coding · Array · Binary Search · Divide and Conquer
- Partition Array Into Two Arrays to Minimize Sum Difference
Coding · Array · Binary Search · Bit Manipulation
- Find First and Last Position of Element in Sorted Array
Coding · Array · Binary Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Meta
Leetcode 4. Median of Two Sorted Arrays
Reported Apr 8, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 4. Median of Two Sorted Arrays
Reported Jan 26, 2025 · 1 source report
INTERN
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 19 records
Amazon
Median of Two Sorted Arrays
Reported date not specified · Source window: 30d
Source snapshot: Oct 20, 2026
Source frequency score: 61.7. This is not an interview probability.
View sourceAmazon
Median of Two Sorted Arrays
Reported date not specified · Source window: 3mo