← Back

Two Sum II — Input Array Is Sorted

The canonical converging scan: one pass, no extra memory.

Medium LeetCode ↗ 1 / 1
func twoSum(numbers []int, target int) []int {

}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.

}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {

    }
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {
        sum := numbers[left] + numbers[right]
    }
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Compare the sum to the target to decide which pointer moves.
    }
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Compare the sum to the target to decide which pointer moves.
        switch {
        case sum == target:
            return []int{left + 1, right + 1} // 1-indexed
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Compare the sum to the target to decide which pointer moves.
        switch {
        case sum == target:
            return []int{left + 1, right + 1} // 1-indexed
        case sum < target:
            left++ // need a bigger sum
        default:
            right-- // need a smaller sum
        }
    }
}
func twoSum(numbers []int, target int) []int {
    // Micropattern: the array is already sorted, so start wide.
    left, right := 0, len(numbers)-1

    // Walk the pointers toward each other.
    for left < right {
        sum := numbers[left] + numbers[right]

        // Compare the sum to the target to decide which pointer moves.
        switch {
        case sum == target:
            return []int{left + 1, right + 1} // 1-indexed
        case sum < target:
            left++ // need a bigger sum
        default:
            right-- // need a smaller sum
        }
    }

    return nil // problem guarantees a solution; unreachable
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.