#K50872. Rearranging Arrays for Minimum Pair Difference Sum
Rearranging Arrays for Minimum Pair Difference Sum
Rearranging Arrays for Minimum Pair Difference Sum
Given two arrays A and B each containing n integers, and a non-negative integer D, your task is to determine whether it is possible to rearrange the elements in A and B such that when we pair the i-th smallest element of A with the i-th smallest element of B, the total sum of absolute differences does not exceed D. In other words, if after sorting A and B in non-decreasing order we have:
(\sum_{i=1}^{n}|a_i - b_i| \le D),
then output True; otherwise, output False.
Note: The optimal strategy is to sort both arrays. This minimizes the total absolute difference between paired elements.
inputFormat
The input is read from standard input (stdin) in the following format:
- An integer n representing the number of elements in each array.
- A line containing n space-separated integers representing array A.
- A line containing n space-separated integers representing array B.
- An integer D.
Note: n can be zero, in which case the arrays are empty.
outputFormat
Output a single line to standard output (stdout) containing either True
if the sum of absolute differences between the paired elements is less than or equal to D, or False
otherwise.
3
1 3 5
5 3 1
4
True