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
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
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
def twosumtwo(a, target)
  return [] if a.length <= 1
  l = 0;
  r = a.length-1
  a.sort!
  while l < r do
    if a[l] + a[r] == target
      return [a[l],a[r]]
    elsif (a[l] + a[r]) < target
      l = l+1
    else
      r = r+1
    end
  end
end
def twosumtwo ( a , target ) return [ ] if a . length <= 1 l = 0 ; r = a . length - 1 a . sort! while l < r do if a [ l ] + a [ r ] == target return [ a [ l ] , a [ r ] ] elsif ( a [ l ] + a [ r ] ) < target l = l + 1 else r = r + 1 end end 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
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
a
<not_specific>
target
<not_specific>