# CtCI - Sum Pair Finder

With an array `[7, 11, 15, 4, 2]` find the indexes that sum up to `9`.

In the first step of the solution we will check if the number at position zero, which in this case is the number seven, add up to the target nine when summed with the other numbers in the array. Below we can see the operations and notice that the solution is `7 + 2` which consists of the numbers in position zero and four.

``````[0, 0] 7 +  7 = 14
[0, 1] 7 + 11 = 18
[0, 2] 7 + 15 = 22
[0, 3] 7 +  4 = 11
[0, 4] 7 +  2 =  9
``````

Since we need two different indexes we can remove the checks where the indexes are the same, in this case we will drop the check at positions `[0, 0]`. So if we have an iterator with `x` as the index of the array, we can assume that `(x == x)` is False.

We already found a solution for the problem, we have that indexes `[0, 4]` add up to the target nine, but lets check the other indexes and see if we can find a pattern to design our algorithm.

``````[1, 0] 11 +  7 = 18
[1, 1] 11 + 11 = 22 # Ignore
[1, 2] 11 + 15 = 26
[1, 3] 11 +  4 = 15
[1, 4] 11 +  2 = 13
``````

We can see that indexes `[0, 1]` and `[1, 0]` are arithmetically the same. This becomes our second rule, we do not need to check the result of summing the numbers in positions that were checked in a previous step. In this case we only need to check the numbers in indexes greater than the current one. If `x = 4` we can ignore the numbers in positions `[0, 1, 2, 3]`.

``````[2, 0] 15 +  7 = 22 # Ignore
[2, 1] 15 + 11 = 26 # Ignore
[2, 2] 15 + 15 = 30 # Ignore
[2, 3] 15 +  4 = 19
[2, 4] 15 +  2 = 17
``````
``````[3, 0] 4 +  7 = 11 # Ignore
[3, 1] 4 + 11 = 15 # Ignore
[3, 2] 4 + 15 = 19 # Ignore
[3, 3] 4 +  4 = 8  # Ignore
[3, 4] 4 +  2 = 6
``````

Finally we find that the last index cannot be checked against any of the other ones because all the other indexes are in a position lower than the last one. Fortunately we already have the pattern for our algorithm, we found that for ever index `x` we are starting the comparison against every number in all the positions of the array starting at `x+1`. If `array[x] + array[x+y] == 9` then we can return `[x, y]` where `y` is any index after position `x`.

``````func solution(numbers []int, target int) []int {
var result = make([]int, 2)
var total int = len(numbers)

for i := 0; i < total; i++ {
for j := (i + 1); j < total; j++ {
if (numbers[i] + numbers[j]) == target {
result = i
result = j
break
}
}
}

return result
}
``````

However, this solution is quite expensive as we are executing an iteration inside another iteration which means that we are doing `n * n` where `n` is the number of elements in the list, which means we are doing `O(n^2)`. We are executing the second iterator because we want to check the first index with all the numbers in the same list, but we are exiting the operation right after the first pair of numbers is found, so it is clear that we don’t need to check all numbers.

Suppose that you have a list of friends and you ask them to come to your house one by one with a outfit of certain color. You want to know if two of your friends will wear the same colors. In our solution we are taking notes of the color of friend `X` and then inviting all the other friends for the color comparison, and doing this every time `X` changes. A real-life solution would be to track the color of each friend in a piece of paper, and every time a new friend comes you check if there is someone in the list (that have already signed) with the same color.

We can do the same here with our exercise. If we are on index zero we find the number `7` which differs from the target `9` by `2` points as `(9 - 7) = 2`. We want to check if the number two is in the tracking list, otherwise we add number seven to the list with its index and continue checking the other positions in the array.

Position two has number `11` which differs from `9` by `-2` points as `(9 - 11) = -2`. We know by a fact that all numbers in the list are unsigned integers and we have to consider this inside the iterator, in his specific case as `-2` is lower than zero we will multiply by `-1` to remove the minus sign. Now we have `2` but unfortunately this number is not in the tracking list as we only have added the number `7` so we have to continue checking the other positions.

By the end of the first iteration we have a hash table `{7:0, 11:1, 15:2, 4:3, 2:4}`

The operations executed so far were like this:

``````Index - Operation - Diff - Abs - Hash Table
     (9 -  7)  =  2     2     {7:0}
     (9 - 11)  = -2     2     {7:0, 11:1}
     (9 - 15)  = -6     6     {7:0, 11:1, 15:2}
     (9 -  4)  =  5     5     {7:0, 11:1, 15:2, 4:3}
     (9 -  2)  =  7     7     {...}
``````

With this operation we want to check if the absolute (unsigned) difference of the subtraction of the number at index `x` from `target` is in the hash table (aka. the tracking list). At iteration zero we want to find the number two in the hash table but it doesn’t exists so we add the number at position zero to the tracking list and continue with the other positions. At iteration four we get `7` as the absolute result of the subtraction, seven is in hash table so we have the solution there, we can pair the `x` index from the original iteration with the index attached to the number seven in the hash table, either `[4, 0]` or `[0, 4]`. With this approach we can do `O(n)`.

``````func solution(numbers []int, target int) []int {
var result = make([]int, 2)
var total int = len(numbers)
var hash = make(map[int]int)
var diff int = 0

for i := 0; i < total; i++ {
diff = target - numbers[i]

if diff < 0 {
diff *= -1
}

if _, ok := hash[diff]; ok {
return []int{hash[diff], i}
}

hash[numbers[i]] = i
}

return result
}
``````
Do you have a project idea? Let's make it together!