Skip to content

Instantly share code, notes, and snippets.

@pwxcoo
Created September 10, 2018 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwxcoo/3475e8e04f1bcf95b98a2a6fe4134557 to your computer and use it in GitHub Desktop.
Save pwxcoo/3475e8e04f1bcf95b98a2a6fe4134557 to your computer and use it in GitHub Desktop.
find Kth number in two sorted arrays.
int findKthSmallest(int vec1[], int n1, int vec2[], int n2, int k)
{
if (n1 == 0)
return vec2[k - 1];
else if (n2 == 0)
return vec1[k - 1];
if (k == 1)
return vec1[0] < vec2[0] ? vec1[0] : vec2[0];
int idx1 = n1 * 1.0 / (n1 + n2) * (k - 1);
int idx2 = k - idx1 - 2;
if (vec1[idx1] == vec2[idx2])
return vec1[idx1];
else if (vec1[idx1] < vec2[idx2])
return findKthSmallest(&vec1[idx1 + 1], n1 - idx1 - 1, vec2, idx2+1, k - idx1 - 1);
else
return findKthSmallest(vec1, idx1+1, &vec2[idx2 + 1], n2 - idx2 - 1, k - idx2 - 1);
}
@pwxcoo
Copy link
Author

pwxcoo commented Sep 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment