Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define a method that returns a boolean indicating whether any two elements in the argument (an array) sum to 0.
- def two_sum_to_zero?(arr)
- i = 0
- while i < arr.length #thereby iterating through the array
- num_one = arr[i]
- j=i+1
- while j <arr.length
- num_two = arr[j]
- if num_one + num_two == 0
- return true
- else
- return false
- end
- j=j+1
- end
- i=i+1
- end
- end
- puts two_sum_to_zero?([1,-1])
- puts two_sum_to_zero?([1,2,3,4])
Add Comment
Please, Sign In to add comment