Member-only story

Solving LeetCode Problem 4: Median of Two Sorted Arrays

Aaliyah.S
3 min readFeb 2, 2024
LeetCode problem 4: Median of Two Sorted Arrays

To solve the problem “Median of Two Sorted Arrays”, we’ll write a Python function that finds the median of two sorted arrays. The median is the middle value in an ordered list of numbers. If the list has an even number of elements, the median is the average of the two middle numbers.

WHY?

We use this algorithm to find the middle value when we have two lists of numbers. This can be especially useful in statistics to understand the middle point of combined data sets.

WHEN AND WHERE?

This problem is common in data analysis, statistics, and software development, and all the more when dealing with sorted datasets and needing to find a central tendency or comparison point.

HOW?

  1. Merge the Arrays: We could simply merge the two arrays into one and then find the median, but this is not efficient for large arrays.
  2. Binary Search: Instead, we use a binary search technique for efficiency. We’ll search for the correct place to “split” both arrays such that the left part has all smaller elements and the right part has all larger elements. The median will be in the middle.

EXPLANATION:

--

--

Aaliyah.S
Aaliyah.S

Written by Aaliyah.S

Just a passionate author, poet, scriptwriter and blogger who wants to change the world with her words. Oh, and a cat lover too!

No responses yet