


n = 4, ARR1 = {1, 2, 3, 4}, ARR2 = {-1, 3, 4, 2}
The maximum value of the expression is obtained when indexes ‘i = 0’ and ‘j = 3’. After evaluating the expression, we get:
|ARR1[0] - ARR2[3]| + |ARR2[0] - ARR2[3]| + |0 - 3| => |1 - 4| + |-1 - 2| + |-3| => |-3| + |-3| + 3 => 9
So the answer is 9.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the length of array ‘ARR1’ and ‘ARR2’.
The second line of each test case contains 'N' space-separated integers denoting the elements of array 'ARR1'.
The third line of each test case contains 'N' space-separated integers denoting the elements of array 'ARR2'.
For each test case, print a single line containing a single integer denoting the maximum value of the given expression.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 1000
-10 ^ 6 <= ARR1[ i ], ARR2[ i ] <= 10 ^ 6
Where ‘T’ is the total number of test cases, ‘N’ denotes the length of arrays 'ARR1' & 'ARR2', and ‘ARR1[i]’ & ‘ARR2[i]’ represents the elements in the respective arrays
Time limit: 1 sec.
We can use two nested loops to iterate through all the possible ‘N * N’ pairs of indexes ‘i’ and ‘j’ to obtain the given expression’s maximum value.
Consider the following properties of the modulus function:
The expression given in the problem is similar to the third equation, after replacing ‘A = ARR1[i] - ARR2[j]’, ‘B = ARR2[i] - ARR2[j]’ and ‘C = i - j’ the given expression can be written as maximum of the following 8 expressions:
In the above expressions, the terms in both brackets are the same. We discuss how to solve equation 1 below. Similarly, we can also solve the remaining equations
Similarly, equations 2&7, 3&6, and 4&5 are virtually the same. So, the given problem’s expression reduces to finding ‘maximum of equation (1, 2, 3, 4)’. Following are the steps to solve the problem: