← DiffPush

sum problem

Standard Bar Arrays · Medium O(n) · O(n)

Reconciling Two Ledger Entries

A reconciliation job receives one batch of transaction amounts and a settlement figure, and exactly one pair of amounts in the batch adds up to that figure. Auditors need the positions of the two entries rather than their values. Each entry may be used at most once, and the pair may be reported in any order.

Input: An array nums of n integers and an integer target.

Output: Two distinct 0-indexed positions i and j with i != j such that nums[i] + nums[j] == target, in any order.

Constraints

Examples

Example 1

Input: {"nums":[2,7,11,15],"target":9}
Output: [0,1]
Positions 0 and 1 hold 2 and 7, which sum to the settlement figure 9.

Example 2

Input: {"nums":[3,2,4],"target":6}
Output: [1,2]
The matching entries sit at positions 1 and 2; the leading 3 is not part of the pair.

Solve this in your browser →

Also on LeetCode ↗