Median of two sorted arrays

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Input: nums1 = [0,0], nums2 = [0,0]
Output: 0.00000
Input: nums1 = [], nums2 = [1]
Output: 1.00000
Input: nums1 = [2], nums2 = []
Output: 2.00000
  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -10^6 <= nums1[i], nums2[i] <= 10^6
class Solution(object):    def findMedianSortedArrays(self, nums1, nums2):        i,j,total=0,0,[]        while(i<len(nums1) or j<len(nums2)):           if i>=len(nums1):               val1=float('inf')           else:               val1=nums1[i]           if j>=len(nums2):               val2=float('inf')           else:               val2=nums2[j]           if val1<val2:               total.append(val1)               i+=1           else:               total.append(val2)               j+=1               length=len(total)        length=len(total)        return([total[length//2],(total[length//2]+total[(length//2)-1])/2][length%2==0])
class Solution(object):   def findMedianSortedArrays(self, nums1, nums2):       pre,curr,ptr1,ptr2=0,0,0,0       cnt=(len(nums1)+len(nums2))//2 + 1       ptr1,ptr2=0,0       while(cnt):          if ptr1==len(nums1):              val1=float('inf')          else:              val1=nums1[ptr1]          if ptr2==len(nums2):              val2=float('inf')          else:              val2=nums2[ptr2]          if val1<val2:              prev=curr              curr=val1              ptr1+=1          else:              prev=curr              curr=val2              ptr2+=1          cnt-=1   return([curr,(prev+curr)/2][(len(nums1)+len(nums2))%2==0])
l1≤r1l1≤r2l2≤r2l2≤r1
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
if len(nums2)<len(nums1):
nums1,nums2=nums2,nums1
total=len(nums1)+len(nums2)
half=total//2
l,h,=0,len(nums1)-1
while(True):
mid=(l+h)//2
cut1=mid
cut2=half-cut1-2
l1 = -1*float('inf') if cut1<0 else nums1[cut1]
l2 = -1*float('inf') if cut2<0 else nums2[cut2]
r1 = float('inf') if cut1>=len(nums1)-1 else nums1[cut1+1]
r2 = float('inf') if cut2>=len(nums2)-1 else nums2[cut2+1]
if l1<=r2 and l2<=r1:
return([min(r1,r2),(max(l1,l2)+min(r1,r2))/2][total%2==0])
elif l1>r2:
h=mid-1
else:
l=mid+1

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Learn Javascript on Udemy (2021)

Server side vs Client side rendering : SEO Optimization

Getting Emotional with EmotionJS

Hello there, @thedapplist is welcoming @walldotapp team for our next episode of Web3 1:1 Talk Show…

Is Angular Component selector a HTML tag or NOT ?

INTERMEDIATE JAVASCRIPT, INTERVIEW QUESTIONS

Structured Logging in a Shell Script with jq

Redux Best Practices for Creating Scaleable API Architectures

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

Systems Engineering in the Ski Industry

Part 1: DataSet preparation | Impact of Pandemic in People’s migration pattern in Chicago

Big Data Valuations: A Pause in a Journey of Learning

Dealing With Data Garbage