Posts

Median of Two Sorted Arrays

  Problem Understanding: In simpler terms, you need to  find the middle value of the combined , sorted array formed by merging nums1 and nums2. If the combined  array has an even number  of elements, you should return the average of the two middle values.  If it has an odd number of elements, you should return the middle value itself. Approach 1: Merge and Sort Create a new array  with a size equal to the total number of elements in both input arrays. Insert elements  from both input arrays into the new array. Sort the new array. Find and return the median of the sorted array. Time Complexity In the worst case TC is  O((n + m) * log(n + m)) . Space Complexity O(n + m) , where ‘n’ and ‘m’ are the sizes of the arrays. Approach 2: Two-Pointer Method Initialize two pointers , i and j, both initially set to 0. Move the pointer  that corresponds to the  smaller value forward at each step. Continue moving the pointers  until you have processed half of the total number of elements. Calculate a
Recent posts