-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path4. Median of Two Sorted Arrays
33 lines (29 loc) · 1.15 KB
/
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
32
33
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
// Get the sizes of both input arrays.
int n = nums1.size();
int m = nums2.size();
// Merge the arrays into a single sorted array.
vector<int> merged;
for (int i = 0; i < n; i++) {
merged.push_back(nums1[i]);
}
for (int i = 0; i < m; i++) {
merged.push_back(nums2[i]);
}
// Sort the merged array.
sort(merged.begin(), merged.end());
// Calculate the total number of elements in the merged array.
int total = merged.size();
if (total % 2 == 1) {
// If the total number of elements is odd, return the middle element as the median.
return static_cast<double>(merged[total / 2]);
} else {
// If the total number of elements is even, calculate the average of the two middle elements as the median.
int middle1 = merged[total / 2 - 1];
int middle2 = merged[total / 2];
return (static_cast<double>(middle1) + static_cast<double>(middle2)) / 2.0;
}
}
};