

There can be more than one possible array with maximum beauty. In that case, you can return any of them.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains an integer ‘N’ denoting the size of arrays.
The second line of each test case contains ‘N’ space-separated integers, representing the elements of the first array, ‘arr1’.
The last line of the test cases contains ‘N’ space-separated integers, representing the elements of the second array, ‘arr2’.
For each test case, the output will be “1” if you have returned the correct answer, else it will be “0”.
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 <= 5
1 <= N <= 5000
1 <= arr1[i], arr2[i] <= 10 ^ 9
Time Limit: 1sec
Check all the possible permutations of ‘arr1’ and return the one with maximum beauty.
The given problem can be viewed as choosing ‘arr1[i]’ from the given elements in ‘arr1’ against ‘arr2[i]’ for each ‘i’ such that beauty can be maximized. So for a given ‘arr2[i]’ we want to choose an element from ‘arr1’ that hasn’t been chosen before such that it should be greater than ‘arr2[i]’. Now suppose there are ‘K’ available elements in ‘arr1’ that are greater than ‘arr2[i]’. It will be best to choose ‘arr1[i]’ as the minimum from these ‘K’ elements because other ‘K-1’ remaining elements can be used for any other ‘arr2[j]’ ( j != 1). But if ‘K’ is 0 i.e. there is no element in ‘arr1’ that is greater than ‘arr2[i]’, then it will be best to choose ‘arr[i]’ as the minimum of all elements remaining in ‘arr1’ because elements other than minimum element can be choose for any other ‘arr2[j]’ (j != i) . So the idea is to iterate through the given ‘arr2’ array and choose the nearest maximum element of each ‘arr2[i]’ from remaining ‘arr1’ if available else choose the minimum element, then remove this element from available ‘arr1’.
In order to maximize the beauty of ‘arr1’ we greedily choose elements from ‘arr1’ for each element in ‘arr2’. For a given element in ‘arr2’ say ‘x’ we need to find an element from ‘arr1’ say ‘y’ such that ‘y’ should be a minimum number that is greater than ‘x’ and ‘y’ must not be chosen before for any element in ‘arr2’. If such ‘y’ do not exist, we select the minimum element from the un-used ‘arr1’ as this will ensure that elements other than minimum element could be chosen for some other ‘x’ in ‘arr2’. To avoid searching for ‘y’ every time for each ‘x’ in ‘arr2’, we can use a multiset data structure to store all elements of ‘arr1’ and efficiently find the upper bound of ‘x’ i.e. finding minimum element greater than ‘x’ in the set.
Suppose for a maximum element in ‘arr2’ say ‘currMax’, we need to select a corresponding element from ‘arr1’. In this case we just need to check the maximum element in ‘arr1’ if it is greater than ‘currMax’, then it will be the corresponding element of ‘currMax’, else there is no element in ‘arr1’ that has a value greater than ‘currMax’, so regardless of selecting any corresponding element, this element does not contribute in the beauty of ‘arr1’. That’s why we will select the minimum element of ‘arr1’ as a corresponding element because elements other than minimum have more chance to contribute to the beauty of ‘arr1’ i.e. other elements may be used as a corresponding element for some elements in ‘arr2’. So the Idea is If the current maximum non-used element in ‘arr1’ is greater than the maximum non-used element in ‘arr2’, we assign the value of ‘arr1’ to the index of the maximum element in ‘arr2’. Or else, we assign the minimum non-used element to the index of the maximum element in ‘arr2’. We can use the max heap to iterate through elements in ‘arr2’. And sort the array ‘arr1’ to find the maximum and minimum element.
As discussed in the previous approach, instead of using heap to order elements of ‘arr2’, we can just simply sort ‘arr2’ while keeping track of indices of each element. And then iterate over ‘arr2’ in descending order and use two pointers in ‘arr1’ to keep track of the current minimum and maximum element in ‘arr1’.