minAbsSumOfTwo.md

July 4, 2025 ยท View on GitHub

Description: Given an array of integers, find the minimal absolute sum of any two elements. Return the minimal absolute sum.

Examples

Example 1:

Input: numbers = [1, 4, -3] Output: 1

Example 2:

Input: numbers = [-8, 4, 5, -10, 3] Output: 1

Example 3:

Input: numbers = [2, 2, 2] Output: 4

Example 4:

Input: numbers = [-1, -2, -3] Output: 2

Example 5:

Input: numbers = [0, 0] Output: 0

Algorithmic Steps This problem is solved using a two-pointer approach after sorting:

  1. Sort the array in non-decreasing order.
  2. Initialize two pointers, left at the start and right at the end of the array.
  3. While left <= right:
    • Compute the sum of numbers[left] and numbers[right].
    • Update the minimal absolute sum if the current sum is closer to zero.
    • Move the pointer that brings the sum closer to zero.
  4. Return the minimal absolute sum found.

Time and Space complexity:

  • Time complexity: O(n log n), where n is the number of elements in the array (due to sorting).
  • Space complexity: O(1) (excluding input storage).