Minimum Absolute Sum Difference - Problem

You're given two positive integer arrays nums1 and nums2, both of length n.

The absolute sum difference of these arrays is defined as the sum of |nums1[i] - nums2[i]| for each index 0 ≤ i < n.

Here's the twist: You can replace at most one element of nums1 with any other element that already exists in nums1 to minimize the absolute sum difference.

Goal: Return the minimum possible absolute sum difference after performing this optional replacement. Since the answer may be large, return it modulo 109 + 7.

Example: If nums1 = [1,7,5] and nums2 = [2,3,5], the initial differences are [1,4,0] with sum = 5. By replacing nums1[1] = 7 with nums1[0] = 1, we get differences [1,2,0] with sum = 3.

Input & Output

example_1.py — Basic Case
$ Input: nums1 = [1,7,5], nums2 = [2,3,5]
Output: 3
💡 Note: Initial differences: [|1-2|, |7-3|, |5-5|] = [1, 4, 0], sum = 5. Replace nums1[1]=7 with nums1[0]=1: new differences = [1, |1-3|, 0] = [1, 2, 0], sum = 3.
example_2.py — No Improvement
$ Input: nums1 = [1,10,4,4,2,7], nums2 = [1,3,5,1,3,4]
Output: 20
💡 Note: Initial sum = |1-1|+|10-3|+|4-5|+|4-1|+|2-3|+|7-4| = 0+7+1+3+1+3 = 15. Best replacement saves 5, giving minimum sum of 20-5+5 = 20... Actually 15 total initially.
example_3.py — Large Numbers
$ Input: nums1 = [1,2,3,4,5], nums2 = [5,4,3,2,1]
Output: 8
💡 Note: Initial differences: [4,2,0,2,4], sum=12. Best improvement is replacing either end element, reducing sum by 4 to get 8.

Constraints

  • n == nums1.length
  • n == nums2.length
  • 1 ≤ n ≤ 105
  • 1 ≤ nums1[i], nums2[i] ≤ 105
  • You can replace at most one element

Visualization

Tap to expand
🏪 Store Price OptimizationYour prices: [1, 7, 5] | Competitor: [2, 3, 5]Current differences: $1 + $4 + $0 = $5 total penaltyStep 1: Sort Your Price CatalogOriginal: [1, 7, 5] → Sorted: [1, 5, 7]This enables fast searching for the best price matchStep 2: Find Best Replacement for Product 2 ($7 vs $3)Binary search in [1, 5, 7] for closest to $3:• $1: difference = $2 (improvement: $4 → $2, save $2)• $5: difference = $2 (improvement: $4 → $2, save $2)Both options save $2 - this is our best improvement!Step 3: Check Other ProductsProduct 1 ($1 vs $2): Best replacement still $1, no improvementProduct 3 ($5 vs $5): Perfect match, no improvement needed🎯 Result: Change product 2 from $7 to $1, new penalty = $3
Understanding the Visualization
1
Assess Current Situation
Calculate total price difference across all products
2
Create Price Catalog
Sort all your existing prices for quick lookup
3
Find Best Match
For each product, find which of your prices best matches competitor
4
Calculate Savings
Determine which price change gives maximum improvement
5
Apply Best Strategy
Make the single change that minimizes total difference
Key Takeaway
🎯 Key Insight: By sorting our price catalog first, we can use binary search to quickly find the optimal price match for each product, reducing time complexity from O(n²) to O(n log n).
Asked in
Google 24 Amazon 18 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen