From a7f2f78849894a076dc68ca3f57c276e4bd75a40 Mon Sep 17 00:00:00 2001 From: luckymachi Date: Sat, 25 Apr 2026 19:58:35 -0500 Subject: [PATCH] two sum - ruby --- arrays_and_hashing/two_sum/main.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 arrays_and_hashing/two_sum/main.rb diff --git a/arrays_and_hashing/two_sum/main.rb b/arrays_and_hashing/two_sum/main.rb new file mode 100644 index 0000000..86da91c --- /dev/null +++ b/arrays_and_hashing/two_sum/main.rb @@ -0,0 +1,19 @@ +def 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 + +nums = [5,5] +target = 10 +puts two_sum(nums, target) -- knotmirror 0.0.1