Tensorum
LeetCode 4 · Two Sorted Arrays / Partition

Median of Two Sorted Arrays

LeetCode 4 · Hard · solution, intuition & step-by-step walkthrough

Binary search the partition: l1 ≤ r2 and l2 ≤ r1.

HardArrayBinary SearchDivide and Conquer
1 msruntime · beats 87%
19.7 MBmemory · beats 8%
2approaches, in order
11accepted runs · last 3 Mar 2026

This is how I solved Median of Two Sorted Arrays (LeetCode 4), a hard problem from the Two Sorted Arrays / Partition pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

The median splits the combined data into two equal halves. Instead of merging, binary search how many elements the left half takes from the smaller array; the rest come from the other. The split is correct when everything on the left is ≤ everything on the right.

Approach

  1. 1

    Brute force: merge-walk to the middle positions.

  2. 2

    Optimal: search mid1 = elements taken from nums1 (the smaller array).

  3. 3

    mid2 = half − mid1; read the border values l1, l2, r1, r2 (±∞ when empty).

  4. 4

    Valid split → median from max(lefts) / min(rights).

  5. 5

    l1 > r2 → take fewer from nums1; else take more.

How I solved it · from first idea to final

1
Merge walkO(m+n) time · O(1) space

Count to the middle.

2
Partition binary searchAcceptedO(log min(m,n)) time

Find the perfect split.

Merge walkPythonO(m+n) time · O(1) space
class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        #brute
        m = len(nums1)
        n = len(nums2)
        mid1 = (m+n)//2
        mid2 = mid1-1
        element1=None
        element2=None
        l=0
        r=0
        count=0
        while l<m and r<n:
            if nums1[l]<=nums2[r]:
                if count==mid1:
                    element1 = nums1[l]
                if count==mid2:
                    element2 = nums1[l]
                l+=1
            else:
                
                if count==mid1:
                    element1 = nums2[r]
                if count==mid2:
                    element2 = nums2[r]
                r+=1
            count+=1
        while l<m:
            if count==mid1:
                element1 = nums1[l]
            if count==mid2:
                element2 = nums1[l]
            count+=1
            l+=1
        while r<n:
            if count==mid1:
                element1 = nums2[r]
            if count==mid2:
                element2 = nums2[r]
            count+=1
            r+=1
        if (m+n)%2==0:
            return (element1+element2)/2
        else:
            return element1
Partition binary searchPythonO(log min(m,n)) time
class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        #brute
        if len(nums1)>len(nums2):
            nums1,nums2 = nums2,nums1
        m = len(nums1)
        n = len(nums2)
        no_of_elements_req_left=(m+n)//2
        low = 0
        high = m
        while low<=high:
            mid1 = (low+high)//2 #no of elements on left
            mid2 = no_of_elements_req_left-mid1
            l1=nums1[mid1-1] if mid1>0 else float("-inf") #last element taken from nums1
            l2=nums2[mid2-1] if mid2>0 else float("-inf") #last element taken from nums2
            r1=nums1[mid1] if mid1<m else float("inf") #first element from nums1
            r2=nums2[mid2] if mid2<n else float("inf") #first element from nums2
            if l1<=r2 and l2<=r1:
                return (max(l1,l2)+min(r1,r2))/2 if (m+n)%2==0 else min(r1,r2)
            elif l1>r2:
                high = mid1-1
            else:
                low = mid1+1
        return -1
Loading the step-through…

Median of Two Sorted Arrays — FAQ

What is the intuition behind Median of Two Sorted Arrays?

The median splits the combined data into two equal halves. Instead of merging, binary search how many elements the left half takes from the smaller array; the rest come from the other. The split is correct when everything on the left is ≤ everything on the right.

How do you solve Median of Two Sorted Arrays (LeetCode 4)?

1. Brute force: merge-walk to the middle positions. 2. Optimal: search mid1 = elements taken from nums1 (the smaller array). 3. mid2 = half − mid1; read the border values l1, l2, r1, r2 (±∞ when empty). 4. Valid split → median from max(lefts) / min(rights). 5. l1 > r2 → take fewer from nums1; else take more.

What is the time and space complexity of Median of Two Sorted Arrays?

Time: O(log min(m, n)). Space: O(1). (Accepted approach: Partition binary search.)

What are common mistakes in Median of Two Sorted Arrays?

Search over the shorter array, or mid2 can go out of range. Use ±∞ sentinels for empty sides.

Is there more than one way to solve Median of Two Sorted Arrays?

Merge walk (O(m+n) time · O(1) space): Count to the middle. Partition binary search (O(log min(m,n)) time): Find the perfect split.