From e58e707a5769b52370081018a1ef552eb29e602f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 11 Mar 2021 09:20:26 -0800 Subject: [PATCH] Add another brute force solution --- data_structures/arrays/two_sum.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/two_sum.rb b/data_structures/arrays/two_sum.rb index 600633e..6fec6fc 100644 --- a/data_structures/arrays/two_sum.rb +++ b/data_structures/arrays/two_sum.rb @@ -26,7 +26,7 @@ # # Approach 1: Brute Force # -# Time Complexity: O(N^2) +# Time Complexity: O(N^2), where N is the length of the array # def two_sum(nums, target) result_array = [] @@ -36,14 +36,36 @@ def two_sum(nums, target) if i != j && i < j current_sum = nums[i] + nums[j] if current_sum == target - result_array.push(i, j) - return result_array + return [i, j] end end end end end +print two_sum([2, 7, 11, 15], 9) +# => [0,1] +print two_sum([3, 2, 4], 6) +# => [1,2] +print two_sum([3, 3], 6) +# => [0,1] + +# +# Approach 2: Define and seek +# +# Time Complexity: O(N^2), where N is the length of the array +# +def two_sum(nums, target) + nums.each_with_index do |num, i| + target_difference = target - nums[i] + nums.each_with_index do |num, j| + if i != j && num == target_difference + return [i, j] + end + end + end +end + print two_sum([2, 7, 11, 15], 9) # => [0,1] print two_sum([3, 2, 4], 6)