-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpair_whose_sum_equals_number.rb
50 lines (41 loc) · 1.02 KB
/
pair_whose_sum_equals_number.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# How to find all pairs on integer array whose sum is equal to given number?
def findPairBrutforce(ar, sum)
pairs = []
length = ar.length
ar.each_with_index do |n1, index|
n2 = sum - n1
ar[index + 1..length].each do |e2|
if n2 == e2
pairs << [n1, n2]
break
end
end
end
pairs.to_s
end
def findPairUsingHash(ar, sum)
pairs = []
ar_hash = {}
# ar.each{|e| ar_hash[e] = true}
ar.each do |e|
ar_hash[e] = ar_hash[e].to_i + 1
pairs << [e, sum - e] if ar_hash[sum - e] != nil
end
pairs.to_s
end
# Leetcode solution
def two_sum(nums, target)
return if nums.empty?
hash = {}
nums.each_with_index do |num, i|
return [i, hash[target - num]] if hash.key?(target - num)
hash[num] = i
end
end
ar = [0, 14, 0, 4, 7, 8, 3, 5, 7]
sum = 11
# puts findPairBrutforce(ar, sum)
# puts findPairBrutforce([2, 4, 3, 5, 6, -2, 4, 7, 8, 9], 7)
puts findPairUsingHash(ar, sum)
puts findPairUsingHash([2, 4, 3, 5, 6, -2, 4, 7, 8, 9], 7)
puts findPairUsingHash([1, 5, 7, -1, 5], 6)