Class: Algoruby::TwoSumSorted

Inherits:
Object
  • Object
show all
Defined in:
lib/algoruby/two_sum/two_sum_sorted.rb

Class Method Summary collapse

Class Method Details

.call(nums, target) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/algoruby/two_sum/two_sum_sorted.rb', line 3

def self.call(nums, target)
  left, right = 0, nums.length - 1

  while left < right
    sum = nums[left] + nums[right]
    if sum == target
      return [nums[left], nums[right]]
    elsif sum < target
      left += 1
    else
      right -= 1
    end
  end

  nil
end