2dbi
Home/Intel/Two Numbers With Sum Closest to Zero
IIntel·DSASDE-1Online Assessment

Two Numbers With Sum Closest to Zero

Problem

Given an array of integers (positive and negative), find the pair whose sum is closest to zero.

Example

[1, 60, -10, 70, -80, 85] -> (-80, 85) sum 5

Constraints

  • 2 ≤ n ≤ 10^5

Approach

Sort, then two pointers tracking the minimum absolute sum.

added 6 days ago
LeadersAccount