Ruby/legacy/DS/Arrays/twosum.rb
# Given an array of integers return check if there exists any two elements 
# whose sum is a given target and the return an array with two elements' indexes
Given an array of integers return check if there exists any two elements
whose sum is a given target and the return an array with two elements' indexes
Given an array of integers return check if there exists any two elements whose sum is a given target and the return an array with two elements ' indexes
def twosum(array, target)
  return [] if array.length<=1
  cache = {}
  array.each.with_index do |e,i|
    if cache.include? (target-e)
      return [i,cache[target-e]]
    else
      cache[e] = i
    end
  end
  return []
end
def twosum ( array , target ) return [ ] if array . length <= 1 cache = { } array . each . with_index do | e , i | if cache . include? ( target - e ) return [ i , cache [ target - e ] ] else cache [ e ] = i end end return [ ] end
Given an array of integers return check if there exists any two elements
whose sum is a given target and the return an array with two elements' indexes
Given an array of integers return check if there exists any two elements whose sum is a given target and the return an array with two elements ' indexes
array
<not_specific>
target
<not_specific>