Question: The question's prompt is: 

> 
> Write a method that takes an array of numbers. If a pair of numbers in the array sums to zero, return the positions of those two numbers. If no pair of numbers sums to zero, return `nil`.
> 
> 
> 

I'm not sure how to approach this problem and what would be the simplest way. Would this involve the .index method?

```
def two_sum(nums)
end

two_sum([1,3,5,-3])

```

Answer: 
As with a lot of things in Ruby, there are a couple different ways of doing this. The "classic" approach to this problem would be to use two nested loops:

```
def two_sum(nums)
  for i1 in 0...nums.length
    for i2 in i1...nums.length
      # Inside this loop, nums[i1] represents one number in the array
      # and nums[i2] represents a different number.
      #
      # If we find that the sum of these numbers is zero...
      if nums[i1] + nums[i2] == 0
        # Then we have our answer
        return i1, i2
      end
    end
  end

  # At this point we've checked every possible combination and haven't
  # found an answer, so a pair of numbers that sum to zero in that array
  # must not exist. Return nil.
  nil
end

```

The other approach uses Ruby magic to do the same thing in a somewhat more expressive way:

```
def two_sum(nums)
  result_pair = nums.each_with_index.to_a.combination(2).find{|n1, n2| n1.first + n2.first == 0}
  result_pair && result_pair.map(&:last)
end

```

The breakdown here is a bit more complex. If you'd like to understand it, I recommend looking at the documentation for these methods on [`Array`](http://ruby-doc.org/core/Array.html) and [`Enumerable`](http://ruby-doc.org/core/Enumerable.html).
Here is another way to do this:

```
def two_sum(nums)
  seen = {}
  nums.each_with_index do |n, i| 
    return seen[-1*n], i if seen.key?(-1*n)
    seen[n] = i
  end
  nil
end

```

It's faster that the above (you do it in one pass) so O(n). 
It does use O(n) additional space compared to the naive approach that checks each element against each element (but that one's O(n^2)