Median of Two Sorted Arrays solution

    
 def findMedianSortedArrays(nums1, nums2):
    merged = sorted(nums1 + nums2)
    n = len(merged)
    if n % 2 == 0:
        mid1 = merged[n // 2]
        mid2 = merged[n // 2 - 1]
        return (mid1 + mid2) / 2
    else:
        return merged[n // 2]

nums1 = [1, 3]
nums2 = [2]
print(findMedianSortedArrays(nums1, nums2))


Explanation:

The "Median of Two Sorted Arrays" problem asks to find the median of the combined sorted arrays nums1 and nums2.

The Python, Java, and C++ solutions follow a similar approach by merging the two arrays into a new sorted array and then finding the median.
Create a merged array that combines nums1 and nums2.
Iterate over nums1 and nums2 with two pointers, comparing the elements and adding the smaller one to the merged array until one of the arrays is fully traversed.
Add the remaining elements from the unfinished array to the merged array.
Determine the median based on the length of the merged array:

If the length is even, average the two middle elements.
If the length is odd, take the middle element.
Return the median value.
The time complexity of the solution is O(m + n), where m and n are the lengths of nums1 and nums2 respectively.

I hope this explanation helps! Let me know if you have any further questions.


         
  class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n = nums1.length + nums2.length;
        int[] merged = new int[n];
        int i = 0, j = 0, k = 0;
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] < nums2[j]) {
                merged[k++] = nums1[i++];
            } else {
                merged[k++] = nums2[j++];
            }
        }
        while (i < nums1.length) {
            merged[k++] = nums1[i++];
        }
        while (j < nums2.length) {
            merged[k++] = nums2[j++];
        }
        if (n % 2 == 0) {
            int mid1 = merged[n / 2];
            int mid2 = merged[n / 2 - 1];
            return (double) (mid1 + mid2) / 2;
        } else {
            return (double) merged[n / 2];
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 3};
        int[] nums2 = {2};
        Solution solution = new Solution();
        System.out.println(solution.findMedianSortedArrays(nums1, nums2));
    }
  }

Explanation:

The "Median of Two Sorted Arrays" problem asks to find the median of the combined sorted arrays nums1 and nums2.

The Python, Java, and C++ solutions follow a similar approach by merging the two arrays into a new sorted array and then finding the median.
Create a merged array that combines nums1 and nums2.
Iterate over nums1 and nums2 with two pointers, comparing the elements and adding the smaller one to the merged array until one of the arrays is fully traversed.
Add the remaining elements from the unfinished array to the merged array.
Determine the median based on the length of the merged array:

If the length is even, average the two middle elements.
If the length is odd, take the middle element.
Return the median value.
The time complexity of the solution is O(m + n), where m and n are the lengths of nums1 and nums2 respectively.

I hope this explanation helps! Let me know if you have any further questions.


         
  #include <iostream>
  #include <vector>
  using namespace std;
    
    double findMedianSortedArrays(vector& nums1, vector& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        vector merged(m + n);
        int i = 0, j = 0, k = 0;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                merged[k++] = nums1[i++];
            } else {
                merged[k++] = nums2[j++];
            }
        }
        while (i < m) {
            merged[k++] = nums1[i++];
        }
        while (j < n) {
            merged[k++] = nums2[j++];
        }
        int mid = (m + n) / 2;
        if ((m + n) % 2 == 0) {
            return (merged[mid - 1] + merged[mid]) / 2.0;
        } else {
            return merged[mid];
        }
    }
    
    int main() {
        vector nums1 = {1, 3};
        vector nums2 = {2};
        cout << findMedianSortedArrays(nums1, nums2) << endl;
        return 0;
    }
    
        
        

Explanation:

The "Median of Two Sorted Arrays" problem asks to find the median of the combined sorted arrays nums1 and nums2.

The Python, Java, and C++ solutions follow a similar approach by merging the two arrays into a new sorted array and then finding the median.
Create a merged array that combines nums1 and nums2.
Iterate over nums1 and nums2 with two pointers, comparing the elements and adding the smaller one to the merged array until one of the arrays is fully traversed.
Add the remaining elements from the unfinished array to the merged array.
Determine the median based on the length of the merged array:

If the length is even, average the two middle elements.
If the length is odd, take the middle element.
Return the median value.
The time complexity of the solution is O(m + n), where m and n are the lengths of nums1 and nums2 respectively.

I hope this explanation helps! Let me know if you have any further questions.

>