MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / pairSumSorted

Function pairSumSorted

go/Two Pointers/pair_sum_sorted.go:1–19  ·  view source on GitHub ↗
(nums []int, target int)

Source from the content-addressed store, hash-verified

1func pairSumSorted(nums []int, target int) []int {
2 left, right := 0, len(nums)-1
3 for left < right {
4 sum := nums[left] + nums[right]
5 // If the sum is smaller, increment the left pointer, aiming
6 // to increase the sum toward the target value.
7 if sum < target {
8 left++
9 } else if sum > target {
10 // If the sum is larger, decrement the right pointer, aiming
11 // to decrease the sum toward the target value.
12 right--
13 } else {
14 // If the target pair is found, return its indexes.
15 return []int{left, right}
16 }
17 }
18 return nil
19}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected