Skip to content

Practice Minimum Absolute Sum Difference

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1818
LeetCode Problem

Minimum Absolute Sum Difference

Problem Statement

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 difference after replacing at most one element in the array nums1. Since the answer may be large, return it modulo 10^9 + 7. |x| is defined as:

Source: Minimum Absolute Sum Difference on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums1 = [1,7,5], nums2 = [2,3,5]
Output
3

# Example 2

Input
nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
Output
0

# Example 3

Input
nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
Output
20

Constraints

n == nums1.length
n == nums2.length
1 <= n <= 10^5
1 <= nums1[i], nums2[i] <= 10^5

Related LeetCode Problems

Practice more problems similar to Minimum Absolute Sum Difference.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.