Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 31, 2021 07:49
Show Gist options
  • Select an option

  • Save lienista/d17ae38f3a52b3876fb71adc02269ed8 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/d17ae38f3a52b3876fb71adc02269ed8 to your computer and use it in GitHub Desktop.
Algorithms in Javascript: Leetcode 4. Median of Two Sorted Arrays - 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)). You may assume nums1 and nums2 cannot be both empty.
const median = (a1, a2) => {
let x = a1.concat(a2);
x.sort(function (a,b) {
return a - b;
});
let len = x.length;
return len%2 === 0 ? (x[Math.floor(len/2)-1] + x[Math.ceil(len/2)])/2 : x[Math.floor(len/2)];
}
let a = [0,2,3,5,9];
let b = [1,4];
console.log(median(a,b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment