Binary Search : Median of two sorted arrays of different sizes.

Tushar Roy - Coding Made Simple · 587K views · 2017 · 24:48

Open on YouTube

Algorithms & data structuresSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining or implementing the partition-based binary search solution for the median of two sorted arrays and related k-th element problems.

Recommended by 2 people across 2 different months, 2018 to 2021.

What it is

A 25-minute whiteboard walkthrough by Tushar Roy of the binary search algorithm for finding the median of two sorted arrays of different lengths in O(log n) time. It explains how to partition the smaller array and derive the matching partition in the larger one, with a linked Java implementation.

What people said

Recommends segue_segway · 2018-05 · Stack Overflow · on “Calculating opposite partition in median of two sorted lists”

From: [LINK] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 I have the following solution (based off of this YouTube video: [this video] ): class Solution: def findMedianSortedArrays(self, x, y): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ x, y = (x, y) if len(x) <= len(y) else (y, x) total_len_even = (len(x) + len(y)) % 2 == 0 left, right = 0, len(x) while left <= right: partition_x = (left + right) // 2 # not sure why we ad

Recommends wstcegg · 2021-04 · Stack Overflow · on “Kth element in two sorted arrays”

Integrate solution: I guess you are working on a really hard problem in leetcode: [LINK] . And there are several good video explanations available on youtube: Tushar, [this video] HuaHua, [LINK] Answer to your concern: The naive solution of this problem would be simply merge two sorted arrays and get the K th element, which is of o(m+n) complexity. Given the keyword sorted and initial linear complexity, it is reasonable to search for a log complexity algorithms, o(log(max(m+n))) . The basic idea is: we can do binary search for the longer array! And the index in the shorter array is automatically identified. To be more specific: There are totally K-1 elements before (smaller that or equal to) the

Cites sam279 · 2020-04 · Stack Overflow · on “Finding the median across two AVL Trees?”

let n , the size of the combined be trees be odd and assume that all the integers across the trees are distinct. Take these two AVL trees as input and find the median of the trees in O(log( n )) time. I've tried and the best I can get is O(log²( n )) time. This is by using a solution algorithm that imitates this question but instead uses two sorted arrays U-tube: Binary Search : Median of two sorted arrays of different sizes [this video] . Could someone please help me find a solution in O(log( n )), and if you provide code, python would be much appreciated! Edit: Each node stores the size of the subtree rooted at that node.

Cites Josh · 2020-05 · Stack Overflow · on “Why have the smaller array drive binary search when computing the median of two sorted arrays?”

A common algorithm for solving the problem of finding the median of two sorted arrays [LINK] of size m and n is to: Run binary search to adjust "a cut" of the smaller array in two halves. When doing so, we adjust the cut of the larger array to make sure the total number of elements on the first halves of both arrays equals the total number of elements in the second halves of both arrays, which is a pre-condition for splitting both arrays around the median. The binary search shifts the cuts left or right until all elements on the left halves <= all elements on the right halves. At the end of the procedure, we can readily compute the median with a basic comparison of the elements on the bounda