dikshantrajput / Hacktoberfest-accepted-2022

This repository is for everyone who wants to participate in Hacktoberfest 2022. Anyone can contribute/add quality code or projects for your Swags (T- Shirt), must be relevant that can add some value to this repository
117 stars 464 forks source link

Create Median of Two Sorted Arrays.py #675

Open DevilANANDGupta opened 1 year ago

DevilANANDGupta commented 1 year ago

class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: n1 = len(nums1) n2 = len(nums2) if n1 > n2: return self.findMedianSortedArrays(nums2, nums1)

l = 0
r = n1

while l <= r:
  partition1 = (l + r) // 2
  partition2 = (n1 + n2 + 1) // 2 - partition1
  maxLeft1 = -2**31 if partition1 == 0 else nums1[partition1 - 1]
  maxLeft2 = -2**31 if partition2 == 0 else nums2[partition2 - 1]
  minRight1 = 2**31 - 1 if partition1 == n1 else nums1[partition1]
  minRight2 = 2**31 - 1 if partition2 == n2 else nums2[partition2]
  if maxLeft1 <= minRight2 and maxLeft2 <= minRight1:
    return (max(maxLeft1, maxLeft2) + min(minRight1, minRight2)) * 0.5 if (n1 + n2) % 2 == 0 else max(maxLeft1, maxLeft2)
  elif maxLeft1 > minRight2:
    r = partition1 - 1
  else:
    l = partition1 + 1