简体   繁体   中英

finding (n*logn) minimal elements from sum of two arrays

I have some problem in my homework, I'm given two arrays A,B (they are sorted) with n elements each one, I need to find (n*logn) minimal elements (or n if it is possible) from the array which cosists of elements which is sum of one element from array A and one element from array B, total number of elemnts in C will be n^2 , - C = {a+b | a belongs to A, b belongs to B} C = {a+b | a belongs to A, b belongs to B} , I need to make it with complexity O(n*logn).
Thanks in advance for any help!

PS I tried to solve it but I have some problems. I know that the first element will be a1 + b1 , a1 first element from A, b1 first element from B.

edited

Also all elements in C are different.

this code below will do the trick it uses the hint you gave 2 elements are sorted and it's order is number of elements needed to find:

int m = n *lon(n);
a[n] = max_int;
b[n] = max_int;

for(int i=0,j=0;i+j<m;)
{
    if(a[i+1]+b[j] < a[i]+b[j+1])
    {
        c[i+j] = a[i+1]+b[j];
        i++;
    }
    else
    {
        c[i+j] = a[i]+b[j+1];
        j++;
    }
}

I'm not giving you the answer, but here are a few hints to get you thinking in the right direction.

So let's say you have. Keep in mind that these lists are already sorted, this is very important. {a1,a2,a3} {b1,b2,b3}

As you correctly said, the first element is a1+b1. Why? Because those are the smallest numbers in both arrays, so their sum will be the smallest.

Now what options do you have for the second element? Remember that the list is sorted! Could it be a1+b2? Could it be a1+b3? And then for the opposite, Could it be b1+a2? Could it be b1+a3?

Could it be b2+a2? (two yes answers and three no answers above)

Using this you should be able to figure out how you find the second smallest element and from here it should be easy to find how to do that for all elements.

If you are still confused, ask questions in the comments.

GL!

Find the smallest element in A (lets call it a) and the smallest element in B (b). The smallest element in C is a + b.

If you need the position of that element in C and if first element is a1 + b1, second a1 + b2 etc. the position of wanted element will be pos(a) * size(B) + pos(b) .

This is a common homework problem. Since the A and B arrays are sorted, you can do a pairwise merge, almost like merge sort, the only difference being that you are trying to compare the sums, not the numbers themselves. In other words, say your pointers are currently at A_i and B_j, and you just added A_i + B_j to the list C. Next element might be A_i + B_{j+1} or it might be A_{i+1} + B_j. Simply see which is smaller, and then move that pointer forward.

It seems you are already on it, and anothem's answer (or rather the questions!) must have already guided you in the right direction, but if you have more questions, feel free to ask.

[PS: No reason to fear duplicates in arrays A, B or C. Duplicates don't change anything for this question. Consider the data structure to be list, instead of set.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM