BugForge
DSA

Pair of Packages

Easyhash map 00:00

A cargo drone can carry exactly capacity kilograms. Given the package weights, return the indices [i, j] (with i < j) of the two packages whose weights sum to exactly capacity.

Exactly one valid pair exists.

Signature

pairSum(weights: int[], capacity: int) → int[]
Example 1
weights = [4,11,7,2,15]
capacity = 9
output = [2,3]
weights[2] + weights[3] = 7 + 2 = 9
Example 2
weights = [3,3]
capacity = 6
output = [0,1]

Constraints

  • 2 ≤ n ≤ 10^5
  • -10^9 ≤ weights[i], capacity ≤ 10^9
  • exactly one answer
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).