my-solutions/problems/1-two-sum/main.rb

16 lines
365 B
Ruby
Raw Normal View History

2022-11-26 17:03:22 +05:00
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer[]}
def two_sum(nums, target)
max_val = nums.max
nums[0...-1].each_with_index do |a, i|
# Skip this number if any sum will be less than target
next if a + max_val < target
nums[i + 1..].each_with_index do |b, j|
return [i, j + i + 1] if a + b == target
end
end
end