MTX Group Online Assessment(Help me in solving this question)

Maximum value

You are given the following:

. An array A consisting of n elements

• An array B consisting of n elements

You can perform the following operations on the array:

• For each you can swap the element A[i] with A[i+1] or A[i-1]

• For each you can swap the element B[i] with B[i+1] or B[i-1]

. Each element can be swapped at most once.

Task

Determine the maximum value of for each i find A[i]xB[i]x(i+1)

Example

Assumptions

• n = 4

• A = {1,2,3,4}

• B={1,3,2,4)

Approach

You can swap A[i], with A[i+1] or A[i-1] and B[i] with B[i+1] or B[i-1] leave an element as it is.

You see that after considering all combinations of A and B. summation of A[i]xB[i]x(i+1) for each i is maximum for A={1,2,3,4},B={1,2,3,4} and the answer is 100.
i.e
(1x1x1)+(2x2x2)+(3x3x3)+(4x4x4)=100

This could simply be done by sorting both the arrays and then finding the summation of products.

Sorting is not allowed as only adjacent element swapping is allowed and each element is swapped atmost once.