Median Mastery – Sorted Arrays, Sorted Mind

 Cutting Right Through the Middle – Finding the Median of Two Sorted Arrays

We’re diving into one of the most talked-about problems in DSA interviews – and not just because it’s labeled “Hard” on LeetCode. The Median of Two Sorted Arrays pushes us to go beyond brute force and embrace optimized divide-and-conquer techniques.

Problem Recap

You’re given two sorted arrays nums1 and nums2. You need to return the median of the two sorted arrays. 

The best data structure for solving it (and why!)

Arrays are the core structure here – no need for fancy DS. But what matters is how you access and partition them efficiently. The optimized approach uses the idea of binary search on partitions, which works perfectly with sorted arrays.

Different approaches – from brute force to optimized solutions

Approach 1: Merge and Find (Brute Force)

Just like merging in merge sort. Combine the arrays, sort them (or just merge them properly), and find the median.


public double findMedianSortedArrays(int[] nums1, int[] nums2) { int[] merged = new int[nums1.length + nums2.length]; int i = 0, j = 0, k = 0; while (i < nums1.length && j < nums2.length) { merged[k++] = nums1[i] < nums2[j] ? nums1[i++] : nums2[j++]; } while (i < nums1.length) merged[k++] = nums1[i++]; while (j < nums2.length) merged[k++] = nums2[j++]; int n = merged.length; if (n % 2 == 0) { return (merged[n / 2 - 1] + merged[n / 2]) / 2.0; } else { return merged[n / 2]; } }

Approach 2: Binary Search on Shorter Array (Optimized – O(log(min(m,n))))

Here comes the brainy bit. Instead of merging, we binary search the partition point in the smaller array and determine if it satisfies the median condition using values from both arrays.


public double findMedianSortedArrays(int[] nums1, int[] nums2) { if (nums1.length > nums2.length) { return findMedianSortedArrays(nums2, nums1); } int x = nums1.length; int y = nums2.length; int low = 0, high = x; while (low <= high) { int partitionX = (low + high) / 2; int partitionY = (x + y + 1) / 2 - partitionX; int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : nums1[partitionX - 1]; int minRightX = (partitionX == x) ? Integer.MAX_VALUE : nums1[partitionX]; int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : nums2[partitionY - 1]; int minRightY = (partitionY == y) ? Integer.MAX_VALUE : nums2[partitionY]; if (maxLeftX <= minRightY && maxLeftY <= minRightX) { if ((x + y) % 2 == 0) { return (Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY)) / 2.0; } else { return Math.max(maxLeftX, maxLeftY); } } else if (maxLeftX > minRightY) { high = partitionX - 1; } else { low = partitionX + 1; } } throw new IllegalArgumentException("Input arrays are not sorted properly"); }

⚠️Common mistakes & pitfalls to avoid

  • Not ensuring nums1 is the smaller array in the optimized approach – binary search should always be done on the shorter one.

  • Edge cases like one array being empty, or arrays of very different sizes.

  • Off-by-one errors when calculating partitions or accessing array indices.

📊Time & Space complexity analysis

Approach 1: Merge and Find

  • Time: O(m + n)

  • Space: O(m + n)

Approach 2: Binary Search Partition

  • Time: O(log(min(m, n)))

  • Space: O(1)

🌍Real-world applications 

  • Statistics engines: Median is a core metric in descriptive stats.

  • Real-time analytics: When dealing with streaming data from two sorted sources (like logs or IoT sensors).

  • Financial data: Median salary or transaction data across datasets.


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries