From 461906d5cafdb8babe838c7ac2a7a9bba2d9c34b Mon Sep 17 00:00:00 2001 From: LostZor0 Date: Thu, 5 Oct 2023 01:11:16 +0530 Subject: [PATCH] fix: Added C code for Median of Two Sorted Arrays #286 --- MedianOfTwoSortedArrays.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 MedianOfTwoSortedArrays.c diff --git a/MedianOfTwoSortedArrays.c b/MedianOfTwoSortedArrays.c new file mode 100644 index 0000000..adb98b1 --- /dev/null +++ b/MedianOfTwoSortedArrays.c @@ -0,0 +1,62 @@ +#include +#include +#include + +double findMedianSortedArrays(int *nums1, int nums1Size, int *nums2, int nums2Size) +{ + if (nums1Size > nums2Size) + { + return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size); + } + + int low = 0, high = nums1Size; + int halfLen = (nums1Size + nums2Size + 1) / 2; + + while (low <= high) + { + int partitionX = (low + high) / 2; + int partitionY = halfLen - partitionX; + + int maxX = (partitionX == 0) ? INT_MIN : nums1[partitionX - 1]; + int minX = (partitionX == nums1Size) ? INT_MAX : nums1[partitionX]; + int maxY = (partitionY == 0) ? INT_MIN : nums2[partitionY - 1]; + int minY = (partitionY == nums2Size) ? INT_MAX : nums2[partitionY]; + + if (maxX <= minY && maxY <= minX) + { + if ((nums1Size + nums2Size) % 2 == 0) + { + return (double)(fmax(maxX, maxY) + fmin(minX, minY)) / 2.0; + } + else + { + return (double)fmax(maxX, maxY); + } + } + else if (maxX > minY) + { + high = partitionX - 1; + } + else + { + low = partitionX + 1; + } + } + + // This should not happen if input arrays are sorted correctly. + return 0.0; +} + +int main() +{ + int nums1[] = {1, 3}; + int nums2[] = {2}; + int nums1Size = sizeof(nums1) / sizeof(nums1[0]); + int nums2Size = sizeof(nums2) / sizeof(nums2[0]); + + double median = findMedianSortedArrays(nums1, nums1Size, nums2, nums2Size); + + printf("Median: %lf\n", median); + + return 0; +}