You are given 2 sorted arrays of size ‘n’ each. You need to stable-merge these arrays such that in the new array sum of product of consecutive elements is maximized. eg A= { 1, 2, 3} B= { 3, 7, 9} Stable merging A and B will give an array C with ’2n’ elements say C={c1, c2, c3, c4, c5, c6} You need to find a new array C by merging (stable) A and B such that sum= c1*c2 + c3*c4 + c5* c6….. n terms is maximum. For the above C={3,7,1,9,2,3}

In fact, I don't think the criterion of getting the greatest sum c1*c2 + c3*c4 + ... is different from simply sorting on order.

And dare to say I think it is the same. If you relax the rules so that you can order freely the elements of A and B into C, the solution to order the ci is optimal.

If the sum contains 2 products a*b and c*d where a,b,c,d are not ordered, you can rearrange them to have a<=b, c<=d and a<=c without changing the sum. If after that you have b>c you can switch b and c, the sum will not decrease. If b>c, (ac+bd) - (ab+cd) = (d-a)(b-c). (a-d) >=0, (b-c) <0, so the expression is <=0. That means (ac+bd) >= (ab+cd). Using this, you can reorder all of C without ever reducing the sum of products.

I'd hazard to guess dynamic programming. At each step you take either an element from A or from B, so in a table that can either down or right. It might be complicated by having to take two steps at a time, but I don't think that should make a fundamental difference.