You are viewing a single comment's thread. Return to all comments →
Here is a Ruby version of your algorithm (where ar is the array of numbers):
def divisibleSumPairs(n, k, ar) pairs = 0 h = Hash.new(0).tap { |h| ar.each { |num| h[num%k] += 1 }} h.each do |key, val| compatikey = k - key compatival = h[compatikey] if key == compatikey || key == 0 pairs += ((1..(val-1)).inject(:+) || 1) if val > 1 elsif compatival > 0 pairs += (val * compatival) end h.delete(key) h.delete(compatikey) end return pairs end
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
Here is a Ruby version of your algorithm (where ar is the array of numbers):