Beyond Big O

Pair-sum indices

Pair-sum indices

Easy

Your task

  • Find two different positions whose values add up to target.
  • Return their indices as [left, right], with left < right.
  • Return an empty list if no pair exists.

Required API

  • PairSumIndices
    func PairSumIndices(values []int, target int) []int

Behavior

  • If several pairs work, choose the smallest right index.
  • If that still leaves a tie, choose the smallest left index.
  • Return indices, not the values at those positions.
  • Separate calls must not share state.
  • Aim for O(n) time and O(n) extra space using only the standard library.

Constraints

  • The input is valid: 0–1,000 integers between -100,000 and 100,000 inclusive.
  • The target is between -200,000 and 200,000 inclusive.
  • Duplicate values are allowed, but you cannot reuse one position.
  • Do not sort or change the input.
Notes and example notation

Examples use {"values": [...], "target": ...}.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · empty

Arguments
values
[]
target
0
Expected return
[]
Test data
Input
{"values":[],"target":0}
Expected output
[]

Example 2 · one pair

Arguments
values
[
  8,
  3,
  6,
  1
]
target
9
Expected return
[
  1,
  2
]
Test data
Input
{"values":[8,3,6,1],"target":9}
Expected output
[1,2]

Example 3 · duplicates

Arguments
values
[
  2,
  2,
  3
]
target
5
Expected return
[
  0,
  2
]
Test data
Input
{"values":[2,2,3],"target":5}
Expected output
[0,2]

Example 4 · no pair

Arguments
values
[
  1,
  4,
  9
]
target
8
Expected return
[]
Test data
Input
{"values":[1,4,9],"target":8}
Expected output
[]

Related guides

Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…