---
# System prepended metadata

title: leetcode 4
tags: [leetcode, ' merge']

---

---
title: leetcode 4
tags: leetcode, merge
---


```c++
// https://leetcode.com/problems/median-of-two-sorted-arrays/
class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int i=0, j=0;
        vector<int> newArray;
        while (i < nums1.size() && j < nums2.size()) {
            if (nums1[i] < nums2[j]) {
                newArray.push_back(nums1[i]);
                i++;
            } else {
                newArray.push_back(nums2[j]);
                j++;
            }
        }
        while (i < nums1.size()) {
            newArray.push_back(nums1[i]);
            i++;
        }
        while (j < nums2.size()) {
            newArray.push_back(nums2[j]);
            j++;
        }
        if (newArray.size() % 2) {
            return newArray[newArray.size()/2];
        } else {
            return (double(newArray[newArray.size()/2-1] + newArray[newArray.size()/2]))/2;
        }
    }
};
```