Given two arrays as:
An array A of size N
An array B of size M
A sequence S is called a Hybrid sequence if it has length N + M and it can be divided into exactly 2 disjoint subsequences A and B.
Among all the possible hybrid sequences S, determine the maximum value of the expression.
[Sum of max(S1) max(S2),....max(SN+M)] - [Sum of min(S1),min(S2),.. min(SN+M)]
Assume indexing is 1 based
Input:
N = 2
A = [4,3]
M = 2
B = [1,2]
Output:
9
The hybrid sequence S = [4,1,2,3] gives the maximum value
Explanation:
[4,1,2,3] -> [max(4)+max(4,1)+max(4,1,2)+max(4,1,2,3)]-[min(4)+min(4,1)+min(4,1,2)+min(4,1,2,3)] = 16-7 = 9
I supposed that max(S)(l-1)-min(S)(l-1), where S= A+B and l is the total number of elements in S, will be the solution, but it failed most of test cases. Can someone explain the problem and maybe solution itself.