From 692b7e3bcd9ed5a7b8a5143c1566793fd009fce9 Mon Sep 17 00:00:00 2001 From: harisjamalkhan111 <90018285+harisjamalkhan111@users.noreply.github.com> Date: Sun, 18 Sep 2022 07:12:41 -0700 Subject: [PATCH] Q3 solution by Haris Jamal Khan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Create a variable count to have a count of elements in the output array. If the value of (m+n) is odd then there is only one median else the median is the average of elements at index (m+n)/2 and ((m+n)/2 – 1). To merge both arrays, keep two indices i and j initially assigned to 0. Compare the ith index of 1st array and jth index of the second, increase the index of the smallest element and increase the count. Store (m+n)/2 and (m+n)/2-1 in two variables. Check if the count reached (m+n) / 2. If (m+n) is odd return m1, If even return (m1+m2)/2. --- Q3/solution.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Q3/solution.py b/Q3/solution.py index 18ab591..a203a13 100644 --- a/Q3/solution.py +++ b/Q3/solution.py @@ -1,3 +1,26 @@ class Solution: - def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + i = 0 # Current index of input array ar1[] + j = 0 # Current index of input array ar2[] + n=len(nums1) + m=len(nums2) + m1, m2 = -1, -1 + for count in range(((n + m) // 2) + 1) : + if(i != n and j != m) : + if nums1[i] > nums2[j] : + m1 = nums2[j] + j += 1 + else : + m1 = nums1[i] + i += 1 + elif(i < n) : + m1 = nums1[i] + i += 1 + # for case when j