Program to find minimum absolute sum difference in Python

Suppose we have two positive valued arrays nums1 and nums2 of the same size. The absolute sum difference of these two arrays is the sum of |nums1[i] - nums2[i]| for each 0 <= i < n (0-indexed). Now, we can replace at most one element of nums1 with any other element in nums1 to minimize the absolute sum difference. We have to find the minimum absolute sum difference after replacing at most one element in the array nums1. The answer may be very large so return it modulo 10^9 + 7.

So, if the input is like nums1 = [2,8,6], nums2 = [3,4,6], then the output will be 3 because we can find two possible optimal solutions ?

  • Replace the element at index 1 with the element at index 0: [2,8,6] => [2,2,6], or
  • Replace the element at index 1 with the element at index 2: [2,8,6] => [2,6,6].

Both of them get a sum difference of |2-3| + (|2-4| or |6-4|) + |6-6| = 3.

Algorithm

To solve this, we will follow these steps ?

  • If nums1 is same as nums2, then return 0
  • Find the index with maximum absolute difference
  • Try replacing that element with every other element in nums1
  • Calculate the minimum possible sum and return it modulo 10^9 + 7

Example

Let us see the following implementation to get better understanding ?

def solve(nums1, nums2):
    if nums1 == nums2:
        return 0

    # Find the index with maximum absolute difference
    max_diff = float('-inf')
    ind = -1
    for i in range(len(nums1)):
        if abs(nums1[i] - nums2[i]) > max_diff:
            ind = i
            max_diff = abs(nums1[i] - nums2[i])
   
    # Find the best replacement for nums1[ind]
    diff = abs(nums1[ind] - nums2[ind])
    index = ind
    for i in range(len(nums1)):
        if i != ind:
            if abs(nums1[i] - nums2[ind]) < diff:
                index = i
                diff = abs(nums1[i] - nums2[ind])

    # Calculate the new sum after replacement
    total_sum = 0
    for i in range(len(nums1)):
        if i == ind:
            total_sum += abs(nums1[index] - nums2[i])
        else:
            total_sum += abs(nums1[i] - nums2[i])
    
    return total_sum % (10**9 + 7)

# Test the function
nums1 = [2, 8, 6]
nums2 = [3, 4, 6]
print(solve(nums1, nums2))

The output of the above code is ?

3

How It Works

The algorithm works by first identifying the position where the absolute difference is maximum. This is the most beneficial position to replace. Then it searches for the best replacement element from nums1 that would minimize the absolute difference at that position. Finally, it calculates the total sum with the optimal replacement.

In our example, the maximum difference occurs at index 1 where |8-4| = 4. We can replace 8 with either 2 or 6 from nums1, both giving us a better result with total sum = 3.

Conclusion

This greedy approach finds the minimum absolute sum difference by replacing the element that contributes most to the current difference. The algorithm runs in O(n²) time and effectively reduces the overall sum by making the optimal single replacement.

Updated on: 2026-03-26T14:40:16+05:30

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements