Leetcode Median of Two Sorted Arrays problem solution in C programming

In the Leetcode Median of Two Sorted Arrays problem solution in C programming Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Leetcode Median of Two Sorted Arrays problem solution in C programming

int cmp(const void *a, const void *b) {
   return ( *(int*)a - *(int*)b );
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){

   
    const int length = nums1Size + nums2Size;
    int i = 0;
    int m = length / 2;
    int* arr = (int*)malloc(length * sizeof(int));
    for(i = 0; i < nums1Size;i++){
        arr[i] = nums1[i];
    }
    for(int d = 0; d < nums2Size;d++){
        arr[i++] = nums2[d];
    }
    qsort(arr,length,sizeof(int),cmp);
   
    
    return length % 2 == 0 ? (double) (arr[m] + arr[m-1]) / 2 : (double) arr[m];
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *