class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1=nums1.length,len2=nums2.length;
int total=len1+len2;
if(total%2==1)
{
return getk(nums1,nums2,total/2+1);
}
else
{
return (getk(nums1,nums2,total/2)+getk(nums1,nums2,total/2+1))/2.0;
}
}
public double getk(int []a,int []b,int k)
{
int lena=a.length;
int lenb=b.length;
int idxa=0,idxb=0;
while(true)
{
if(idxa==lena)return b[idxb+k-1];
if(idxb==lenb)return a[idxa+k-1];
if(k==1)return Math.min(a[idxa], b[idxb]);
int newidxa=Math.min(lena-1, idxa+k/2-1);
int newidxb=Math.min(lenb-1, idxb+k/2-1);
int p1=a[newidxa];int p2=b[newidxb];
if(p1<=p2)
{
k-=(newidxa-idxa+1);
idxa=newidxa+1;
}
else
{
k-=(newidxb-idxb+1);
idxb=newidxb+1;
}
}
}
}