Hello!

The first input parameter will be a list of numbers, the second will be a number - the sum you need to find. The essence of the problem is to find which pair of numbers in the sum is equal to the second argument.

There are several possible solutions for solving this problem, we will consider the option with two pointers. To do this, we will need a sorted list and two pointers, one pointing to the first item in the list and the other to the last.

For example, we will take this list 8, -2, 11, 13, -1, 6, 4 and the sum which needs to be found 15. After sorting we get:

>-2, -1, 4, 6, 8, 11, 13<

One pointer points to -2 and the other to 13. We calculate the sum of these elements -2 + 13 = 11 which is less than the amount we are looking for (15). So the left pointer must be moved forward

-2, >-1, 4, 6, 8, 11, 13<

We look for the sum -1 + 13 = 12 and again it less than 15, therefore we move the left pointer on 1 position to the right

-2, -1, >4, 6, 8, 11, 13<

Again, count the sum of 4 + 13 = 17, which is more than 15 means the right pointer must be moved to the left

-2, -1, >4, 6, 8, 11< , 13

We calculate the sum 4 + 11 = 15 which is equal to the sum we are looking for and therefore the result will be 4, 11

An example of solving the problem on go

package main

import (
	"fmt"
	"sort"
)

func main() {
	fmt.Println(twoNumberSum([]int{8, -2, 11, 13, -1, 6, 4}, 15))
}

func twoNumberSum(array []int, targetSum int) []int {
	sort.Ints(array)

	left := 0
	right := len(array) - 1

	for {
		if left >= right {
			break
		}

		currentSum := array[left] + array[right]

		if currentSum == targetSum {
			return []int{array[left], array[right]}
		} else if currentSum < targetSum {
			left++
		} else {
			right--
		}
	}

	return []int{}
}