Skip to content

Commit

Permalink
Add 4-Median-of-Two-Sorted-Arrays.c
Browse files Browse the repository at this point in the history
Create the C solution similar to the YouTube video solution "https://www.youtube.com/watch?v=q6IEA26hvXc"
  • Loading branch information
MHamiid authored Oct 16, 2022
1 parent fd465a1 commit 97e712c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions c/4-Median-of-Two-Sorted-Arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Time: log(min(n, m))
Space: O(1)
*/

#define min(x, y) ((x < y) ? (x) : (y))
#define max(x, y) ((x > y) ? (x) : (y))

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
int* A = nums1;
int* B = nums2;
int ASize = nums1Size;
int BSize = nums2Size;

int total = nums1Size + nums2Size;
int half = total / 2;

if(nums2Size < nums1Size)
{
A = nums2;
B = nums1;
ASize = nums2Size;
BSize = nums1Size;
}

int l = 0;
int r = ASize - 1;

while(true)
{
int i = l + ((r - l - 2 + 1) / 2); // A Mid, round down instead of rounding towards 0
int j = half - i - 2; // B Mid


int Aleft = i >= 0 ? A[i] : INT_MIN;
int Aright = (i + 1) < ASize ? A[i + 1] : INT_MAX;
int Bleft = j >= 0 ? B[j] : INT_MIN;
int Bright = (j + 1) < BSize ? B[j + 1] : INT_MAX;

// partition is correct
if(Aleft <= Bright && Bleft <= Aright)
{
// Odd
if(total % 2)
{
return min(Aright, Bright);
}

// Even
return (max(Aleft, Bleft) + min(Aright, Bright)) / 2.0;
}
else if(Aleft > Bright)
{
r = i - 1;
}
else
{
l = i + 1;
}
}

}

0 comments on commit 97e712c

Please sign in to comment.