You are given two positive integer arrays nums1 and nums2, both of length
n.
The absolute sum difference of arrays nums1 and nums2 is defined as the sum of |nums1[i] - nums2[i]| for each 0 <= i < n (0-indexed).
You can replace at most one element of nums1 with any other element in nums1 to minimize the absolute sum difference.
Return the minimum absolute sum differenceafter replacing at most one**** element in the array nums1. Since the answer may be large, return it
modulo10^9 + 7.
Input: nums1 =[1,7,5], nums2 =[2,3,5]Output: 3Explanation: There are two possible optimal solutions:- Replace the second element with the first:[1,_**7**_ ,5]=>[1,_**1**_ ,5], or
- Replace the second element with the third:[1,_**7**_ ,5]=>[1,_**5**_ ,5].Both will yield an absolute sum difference of |1-2|+(|1-3| or |5-3|)+|5-5|=3.
Input: nums1 =[2,4,6,8,10], nums2 =[2,4,6,8,10]Output: 0Explanation: nums1 is equal to nums2 so no replacement is needed. This will result in an
absolute sum difference of 0.
Input: nums1 =[1,10,4,4,2,7], nums2 =[9,3,5,1,7,4]Output: 20Explanation: Replace the first element with the second:[_**1**_ ,10,4,4,2,7]=>[_**10**_ ,10,4,4,2,7].This yields an absolute sum difference of |10-9|+|10-3|+|4-5|+|4-1|+|2-7|+|7-4|=20
To minimize the absolute sum difference, we can replace at most one element in nums1 with another element from nums1. For each index, we want to find the closest value in nums1 to nums2[i] to minimize the difference. Using a sorted set allows us to efficiently find the closest value for each nums2[i].
funcminAbsoluteSumDiff(nums1 []int, nums2 []int) int {
n, mod:= len(nums1), int(1e9+7)
sorted:= append([]int{}, nums1...)
sort.Ints(sorted)
total, maxRed:=0, 0fori:=0; i < n; i++ {
diff:=abs(nums1[i]-nums2[i])
total = (total+diff) %modidx:=sort.SearchInts(sorted, nums2[i])
best:=diffifidx < n {
ifabs(sorted[idx]-nums2[i]) < best {
best = abs(sorted[idx]-nums2[i])
}
}
ifidx > 0 {
ifabs(sorted[idx-1]-nums2[i]) < best {
best = abs(sorted[idx-1]-nums2[i])
}
}
ifdiff-best > maxRed {
maxRed = diff-best }
}
return (total-maxRed+mod) %mod}
funcabs(xint) int { ifx < 0 { return-x }; returnx }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
publicintminAbsoluteSumDiff(int[] nums1, int[] nums2) {
int n = nums1.length, mod = 1000000007;
int[] sorted = nums1.clone();
Arrays.sort(sorted);
int total = 0, maxRed = 0;
for (int i = 0; i < n; ++i) {
int diff = Math.abs(nums1[i]- nums2[i]);
total = (total + diff) % mod;
int idx = Arrays.binarySearch(sorted, nums2[i]);
int best = diff;
if (idx < 0) idx =-idx - 1;
if (idx < n) best = Math.min(best, Math.abs(sorted[idx]- nums2[i]));
if (idx > 0) best = Math.min(best, Math.abs(sorted[idx-1]- nums2[i]));
maxRed = Math.max(maxRed, diff - best);
}
return (total - maxRed + mod) % mod;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funminAbsoluteSumDiff(nums1: IntArray, nums2: IntArray): Int {
val n = nums1.size
val mod = 1_000_000_007
val sorted = nums1.sorted()
var total = 0var maxRed = 0for (i in0 until n) {
val diff = kotlin.math.abs(nums1[i] - nums2[i])
total = (total + diff) % mod
val idx = sorted.binarySearch(nums2[i]).let { if (it < 0) -it-1elseit }
var best = diff
if (idx < n) best = minOf(best, kotlin.math.abs(sorted[idx] - nums2[i]))
if (idx > 0) best = minOf(best, kotlin.math.abs(sorted[idx-1] - nums2[i]))
maxRed = maxOf(maxRed, diff - best)
}
return (total - maxRed + mod) % mod
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defmin_absolute_sum_diff(nums1: list[int], nums2: list[int]) -> int:
from bisect import bisect_left
mod =10**9+7 sorted_nums1 = sorted(nums1)
total =0 max_red =0for a, b in zip(nums1, nums2):
diff = abs(a - b)
total = (total + diff) % mod
idx = bisect_left(sorted_nums1, b)
best = diff
if idx < len(sorted_nums1):
best = min(best, abs(sorted_nums1[idx] - b))
if idx >0:
best = min(best, abs(sorted_nums1[idx-1] - b))
max_red = max(max_red, diff - best)
return (total - max_red + mod) % mod