Boolean to check if array contains a pair that sums to 0
I'm doing a simple ruby challenge, which asked me to create a method
two_sum?(arr), that returns true if the array contains 2 pairs which add
up to 0. This can be 0 and 0, or -2 and + 2 etc. This challenge does not
expect me to know .permutation, so I tried to do it this way --
def two_sum?(arr)
arr.each do | obj |
arr.each do | pair_obj |
return true if obj + pair_obj == 0
end
end
false
end
When I run this with sample arrays however, I get true for any array that
I pop in.
Any help in what I'm doing wrong? Is it not allowed to do arr.each twice?
No comments:
Post a Comment