


You are given two arrays of the same size. Your task is to maximize the possible sum that can be calculated using elements of the two given arrays. You are allowed to swap elements between the arrays at the same position any number of times to achieve this.
Note:The maximum sum is calculated by using the following rules:-
For every ‘i’ in range 0...N:
Max_sum += abs(arr1[i] - arr2[i])
And If i < N-1:
Max_sum += abs(arr1[i+1] - arr2[i])
For Example
Input:
n = 4
arr1[] = {2,3,4,1}
arr2[] = {2,4,1,1}
Ouput: 11
Explanation:
Here we will swap arr1[2] and arr2[2]. (Swap)
arr1[] = {2,3,1,1}
arr2[] = {2,4,4,1}
for this type of arrangement, we have our maximum Define Sum.
Sum = abs(arr1[0]-arr2[0]) + abs(arr1[1]-arr2[0]) + abs(arr1[1]-arr2[1]) + abs(arr1[2]-arr2[1]) ....
Sum = abs(2-2) + abs(3-2) + abs(3-4) + abs(1-4) + abs(1-4) + abs(1-4) + abs(1-1) = 11
The first line of input contains a single integer ‘T’ denoting the number of test cases that would be there.
The first line of each test case contains a single integer ‘N’ denoting the number of integers that would be given in both the lists (arrays).
The next two lines contain ‘N’ space-separated integers denoting the elements of the two lists which would be given.
Output Format :
Return maximised possible sum which can be calculated using the elements of the two given lists as depicted by the formula mentioned above.
1 <= T <= 100
1 <= N <= 5000
1 <= nums[i] <= 10^6
Time Limit: 1sec
2
4
2 2 4 3
2 4 2 3
5
5 5 6 6 5
1 2 5 4 3
9
24
Here we will swap arr1[2] and arr2[2]. (Swap)
arr1[] = {2,4,4,3}
arr2[] = {2,2,2,3}
for this type of arrangement, we have our maximum Define Sum.
Sum = abs(arr1[0]-arr2[0]) + abs(arr1[1]-arr2[0]) + abs(arr1[1]-arr2[1]) + abs(arr1[2]-arr2[1]) ....
Sum = abs(2-2) + abs(4-2) + abs(4-2) + abs(4-2) + abs(4-2) + abs(3-2) + abs(3-3) = 9
Here we will swap arr1[3] and arr2[3] and Also swap arr1[4] and arr2[4]. (Swap)
arr1[] = {5,5,6,4,3}
arr2[] = {1,2,5,6,5}
for this type of arrangement, we have our maximum Defined Sum.
Sum = abs(arr1[0]-arr2[0]) + abs(arr1[1]-arr2[0]) + abs(arr1[1]-arr2[1]) + abs(arr1[2]-arr2[1]) ....
Sum = abs(5-1) + abs(5-1) + abs(5-2) + abs(6-2) + abs(6-5) + abs(6-5) + abs(4-6) + abs(3-6) + abs(3-5) = 24
1
4
2 3 4 1
2 4 1 1
11
Try to find a pattern of solving repeated sub-problems and use dynamic programming to solve them.
The idea here is to maximise the sum possible by choosing whether to swap the list elements between the two given lists (arrays) or not. if the array elements are swapped and the sum is maximised, then we perform swapping otherwise we calculate the maximum sum for the ‘i’th element without any swapping and move on to calculating the sum for the remaining elements.
The algorithm will be-
O(N),
where ‘N’ denotes the number of items given in the problem for the two lists.
Since we are iterating through all the elements in the problem therefore, the time complexity will be O(N).
O(N),
where ‘N’ denotes the number of items given in the problem.
We iterate through the list (array) and construct a 2D list (array) ‘dp’ of elements and count O(N). That is why the space complexity is O(N).