ruby-solutions/two_sum_2_input_array_sorted.rb
def two_sum(numbers, target)
  results = []

  if numbers === nil || numbers.length === 0
   return results
  end

  i = 0
  j = numbers.length - 1

  while i < j
    sum = numbers[i] + numbers[j]
    if sum === target
      results[0] = i + 1
      results[1] = j + 1
      break
    elsif sum < target
      i+=1
    else
      j-=1
    end
  end  
  p results    
end
# Psuedocode
# create an empty array
# loop with 2 pointers( the first element and the last element)
#  create a sum variable
#  if sum is equal to the target assign i and j as the index number
#  if the sum is less than the target, add 1 to i to point to the next index
#  if the sum is greater than the target subract 1 from j to point to the next to last element 
Psuedocode
create an empty array
loop with 2 pointers( the first element and the last element)
create a sum variable
if sum is equal to the target assign i and j as the index number
if the sum is less than the target, add 1 to i to point to the next index
if the sum is greater than the target subract 1 from j to point to the next to last element
Psuedocode create an empty array loop with 2 pointers ( the first element and the last element ) create a sum variable if sum is equal to the target assign i and j as the index number if the sum is less than the target add 1 to i to point to the next index if the sum is greater than the target subract 1 from j to point to the next to last element
def two_sum(numbers, target)
  results = []
  if numbers === nil || numbers.length === 0
   return results
  end
  i = 0
  j = numbers.length - 1
  while i < j
    sum = numbers[i] + numbers[j]
    if sum === target
      results[0] = i + 1
      results[1] = j + 1
      break
    elsif sum < target
      i+=1
    else
      j-=1
    end
  end  
  p results    
end
def two_sum ( numbers , target ) results = [ ] if numbers === nil || numbers . length === 0 return results end i = 0 j = numbers . length - 1 while i < j sum = numbers [ i ] + numbers [ j ] if sum === target results [ 0 ] = i + 1 results [ 1 ] = j + 1 break elsif sum < target i += 1 else j -= 1 end end p results end
Psuedocode
create an empty array
loop with 2 pointers( the first element and the last element)
create a sum variable
if sum is equal to the target assign i and j as the index number
if the sum is less than the target, add 1 to i to point to the next index
if the sum is greater than the target subract 1 from j to point to the next to last element
Psuedocode create an empty array loop with 2 pointers ( the first element and the last element ) create a sum variable if sum is equal to the target assign i and j as the index number if the sum is less than the target add 1 to i to point to the next index if the sum is greater than the target subract 1 from j to point to the next to last element
numbers
target