You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`double median(int arr1[], int arr2[], int n, int m) {
// If the arrays are empty, return -1.
if (n == 0 || m == 0) {
return -1;
}
// Merge the two arrays into a new array.
int *mergedArray = new int[n + m];
int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (arr1[i] <= arr2[j]) {
mergedArray[k++] = arr1[i++];
} else {
mergedArray[k++] = arr2[j++];
}
}
// Copy the remaining elements of arr1 to mergedArray.
while (i < n) {
mergedArray[k++] = arr1[i++];
}
// Copy the remaining elements of arr2 to mergedArray.
while (j < m) {
mergedArray[k++] = arr2[j++];
}
// Calculate the median of the merged array.
int medianIndex = (n + m) / 2;
if ((n + m) % 2 == 0) {
// If the number of elements is even, the median is the average of the two middle elements.
return (mergedArray[medianIndex - 1] + mergedArray[medianIndex]) / 2.0;
} else {
// If the number of elements is odd, the median is the middle element.
return mergedArray[medianIndex];
}
}
int main() {
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {2, 4, 6, 8, 10};
int n = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
double median = median(arr1, arr2, n, m);
printf("The median of the two arrays is: %f\n", median);
return 0;
}
`
this is the c++ code for calculating median of 2 sorted arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolve #1
Fixes #1