Yahoo India Web Search

Search results

  1. Median of Two Sorted Arrays. Hard. Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000. Explanation: merged array = [1,2,3] and median is 2. Example 2:

    • Discuss (999+)

      Median of Two Sorted Arrays - Level up your coding skills...

  2. May 2, 2024 · Learn how to find the median of two sorted arrays with unequal sizes using various approaches. See examples, illustrations, code and complexity analysis.

    • 26 min
  3. 4. Median of Two Sorted Arrays. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31. class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { const int n1 = nums1.size(); const int n2 = nums2.size(); if (n1 > n2) return findMedianSortedArrays(nums2, nums1); int l = 0;

  4. May 9, 2023 · Learn how to find the median of the array obtained after merging two sorted arrays of size n each in O (log n) time. See different methods, implementations and examples in C, C++, Java, Python and other languages.

    • 26 min
    • Let's Understand The Problem
    • Discussed Solution Approaches
    • Brute Force Approach: Merging Using Extra Space
    • Two-pointers Approach: Counting While Merging
    • Efficient Approach: Divide and Conquer Idea Similar to Binary Search
    • Critical Ideas to Think!
    • Comparison of Time and Space Complexities
    • Suggested Coding Problems to Practice

    There are two sorted arrays A and B of size n each. Write a program to find the median of the array obtained after merging both sorted arrays, i.e., the merged array of size 2n. 1. The median of a sorted array of size n is defined as the middle element when n is odd and the average of the two middle elements when n is even. 2. After merging both so...

    Brute force approach: Merging using extra space
    Two-pointer approach: Counting while merging
    Efficient approach: Divide and conquer idea similar to binary search

    Solution idea

    One basic idea is to merge both sorted arrays using extra space and get the median by taking the average of the two middle elements in the merged array. If we follow 0-based indexing, the median will be the average of the values at the (n-1)th and nth indexes. Note: For merging sorted arrays, we can use an approach similar to the merging procedure of merge sort.

    Time and space complexity analysis

    We need to traverse each element to merge both sorted arrays. In other words, at each iteration of the while loop, we place one value to MergedArr. So the time complexity of merging loop = O(n). The overall time complexity = Time complexity of merging + Time complexity of finding median = O(n) + O(1) = O(n). We are using 2n size extra space for merging, so space complexity = O(n).

    Solution idea

    Now, the critical questions are: Do we really need to perform a complete merge of sorted arrays to find the median? Can we reduce the extra space used in the merging process? Let's think! Here are a few critical observations: We are traversing both arrays from the start in incremental order. At each iteration step, we are either increasing pointer i or pointer j and placing one value from either of the smaller arrays into the larger sorted array. At any general step of iteration, k = i + j -...

    Solution steps

    1. We initialize two pointers i and jto move forward in A and B. i = 0 and j = 0. 2. We initialize three variables: middle1 and middle2 to keep track of the middle elements and countto track the count of elements in the merged array. middle1 = 0, middle2 = 0 and count = 0. 3. Now we start the merging loop and move forward by comparing elements in A and B until the countbecomes equal to the n + 1. We also keep track of middle elements middle1 and middle2. 4. By the end of the loop, we...

    Time and Space complexity analysis

    We are running while loop n + 1 times and performing O(1) operation at each iteration. So the time complexity = (n + 1)* O(1) = O(n). Since we are using a constant number of extra variables, space complexity = O(1).

    Now, the critical questions are: How can we improve the time complexity? Since this is a searching problem, can we take advantage of the sorted array property? Let's think!

    Can we solve this problem in O(logn) time complexity using an iterative approach?
    How do we modify the above algorithms when n is even?
    How do we modify the above algorithms when n can be both odd or even?
    How can we solve the above problem if the size of both arrays is different? What would be the time complexity? What are the boundary conditions?
    Merging using extra space: Time = O(n), Space = O(n).
    Using a two-pointer approach: Time = O(n), Space = O(1).
    Using divide and conquer approach: Time = O(logn), Space = O(logn).
    Median of two sorted arrays of different sizes
    K-th element of two sorted arrays
    Josephus problem
  5. Mar 26, 2021 · Here, we have two sorted arrays A and B. In order to find the median of these arrays, we can need to combine these two arrays, sort it and compute the median of the combined...

  6. People also ask

  7. Learn how to find the median of two sorted arrays using binary search and divide and conquer algorithms. See the problem description, intuition, solution approach, and example walkthrough on Algo.monster.

  1. People also search for