Directi Interview Question
Software Engineer / Developersbiggest*second biggest + ... + second smallest*smallest is the solution.
Possibility of negative numbers in both the arrays have to be taken care of.
negative*negative = positive.
Goal is that C should be sorted. Thats it. try it with sample inputs, all negative, some negative and positive and all positives. It works.
Just Use Merge operation of merge sort even wothout sorting individual arrays.
for arrays 3,7,1
6,8,2
it will result in 3,6,7,1,8
Above solution wont work. For the given case, output should be 3,6,7,8,1,2 to maximize the sum of products.
Here is one logic :
Start from first element of array A[], now to maintain stable merge as well as maximum sum property choose max(next element, Ist element of B[]), now do in this way merging for all next element, it can give you array C[] which is stable merge as well as have max sum of product of adjecant elements.
Sorting the arrays individually in ascending order and then merging them by appending the second to the first would do.
- Kalpana Jalawadi February 16, 2010