Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Median Of Two Sorted Arrays

Medium Acceptance 71.00% Points 30.00

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.

Hints
Hint 1
N/A
Constraints
  • 0 = m, n = 1000
  • 1 = m + n = 2000
  • -106 = nums1[i], nums2[i] = 106
Companies
Amazon
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions