Recently in the interview was asked an algorithm topic:
Given two ordered arrays a[] and b[], find the a[i]+b[j] of the K small.
Unfortunately, there is no answer on the spot ...
Back to check, in fact, this topic can also be converted to the young matrix in the K-small value. Do not know what is the young matrix, you can own Baidu. or simply as a matrix that is incremented for each column in each row.
This problem can be solved by the binary method, the specific algorithm is as follows:
public int kthsum (int[] A, int[] b,int k) {int lena=a.length;
int lenb=b.length;
int max=a[lena-1]+b[lenb-1];
int min=a[0]+b[0];
return solve (A, B,max, Min, k);
public int Solve (int[] a,int [] b,int max,int min, int k) {if (max==min) return Max;
int mid= (MIN+MAX)/2;
if (Counter (mid, A, b) >=k) return solve (A, B, Max, mid+1, K);
if (Counter (mid-1, A, b) <k) return solve (A, B, Mid-1, Min, k);
return mid;
public int counter (int val,int[] a,int[] B) {int i=0;
int j=b.length-1;
int count=0;
while (i<a.length&&j>=0) {if (a[i]+b[j]>val) {count = A.length-i;
j--;
}else {i++;
} return count; }