BackeasyTwo Pointers

Finding Symmetric Opposites Solution

Problem Statement

You are given a sorted array of unique integers 'nums'. Your task is to find a pair of distinct elements in the array that are additive inverses of each other (their sum is exactly 0). Return the pair as a list [a, b] where a < b. If no such pair exists, return an empty list.

Example 1
Input
nums = [-4, -2, 1, 2, 5]
Output
[-2, 2]

Explanation: The elements -2 and 2 are present in the array, and their sum is -2 + 2 = 0.

Example 2
Input
nums = [-10, -5, 0, 3, 7]
Output
[]

Explanation: There is no pair of distinct elements in the array that sum up to 0.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • nums is sorted in strictly increasing order.
Live Compiler
Sign InSign Up
Loading...
Test Cases & Output
Click "Run" to execute