Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Median Of Two Sorted Arrays

Medium Acceptance 71% Points 30

You are given two sorted arrays nums1[] and nums2[] of sizes m and n. Your task is to find the median of the two sorted arrays. The median is the middle element when all elements from both arrays are combined and arranged in sorted order. If the total number of elements is even, the median is the average of the two middle elements.

Examples
Example 1

Example 1

Input: nums1 = [1,3], nums2 = [2]

Output: 2.0

Explanation: Combined sorted array is [1,2,3]. The middle element is 2, so the median is 2.0.

Example 2

Example 2

Input: nums1 = [1,2], nums2 = [3,4]

Output: 2.5

Explanation: Combined sorted array is [1,2,3,4]. The two middle elements are 2 and 3. The median is (2 + 3) / 2 = 2.5.

Example 3

Example 3

Input: nums1 = [0,0], nums2 = [0,0]

Output: 0.0

Explanation: Combined sorted array is [0,0,0,0]. The two middle elements are 0 and 0. The median is (0 + 0) / 2 = 0.0.

Constraints
  • 0 = m, n = 1000
  • 1 = m + n = 2000
  • -106 = nums1[i], nums2[i] = 106
Topics
Binary Search
Companies
Amazon
Added 22 Jul 2026 71% acceptance
Ln 1, Col 1Sign in to save your code
1 3
2
2.0