Two Sum Ii

# Given an array of integers numbers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

# Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length.

# You may assume that each input would have exactly one solution and you may not use the same element twice.

#
# Approach 1: Two pointers
#

# Complexity analysis

# Time complexity: O(n). Each of the n elements is visited at
# most once, thus the time complexity is O(n).

# Space complexity: O(1). We only use two indexes, the space
# complexity is O(1).

def two_sum(numbers, target)
  i = 0
  j = numbers.length - 1

  while i < j
    sum = numbers[i] + numbers[j]

    if target < sum
      j -= 1
    elsif target > sum
      i += 1
    else
      return [i + 1, j + 1]
    end
  end
end

nums = [2, 7, 11, 15]
target = 9
print(two_sum(nums, target))
# => [1,2]

nums = [2, 3, 4]
target = 6
print(two_sum(nums, target))
# => [1,3]

nums = [-1, 0]
target = -1
print(two_sum(nums, target))
# => [1,2]
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.