Something went wrong. Try again.
Neetcode 150 solutions in different langs
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233def two_sum(nums, target) nums_map = Hash.new nums.each_with_index do |num, idx| nums_map.store(num, idx) end
nums.each_with_index do |num, idx| diff = target - num if nums_map.has_key?(diff) and nums_map[diff] != idx return [idx, nums_map[diff]] end end
[]end
sum_file = File.readlines("input.txt")
nums = []target = -1
sum_file.each_with_index do |line, index| if index == 0 line.split(" ").each do |num| nums.push(num.to_i) end else target = line.to_i endend
puts two_sum(nums, target)