Hacker News new | ask | show | jobs
by sheriff 3711 days ago
Here's an O(N) solution to the `findSum` problem in Ruby:

  def findSum(array1, array2, sum)
    complements = array1.map{|x| sum - x}.reverse

    i = complements.count - 1
    j = array2.count - 1

    while i >= 0 && j >= 0
      return true if complements[i] == array2[j]

      if complements[i] > array2[j]
        i -= 1
      else
        j -= 1
      end
    end

    false
  end