Skip to content

Instantly share code, notes, and snippets.

@JuliusNM
Created February 18, 2020 07:04
Show Gist options
  • Save JuliusNM/8bc81aa0ff861097cb7886ea622a3d76 to your computer and use it in GitHub Desktop.
Save JuliusNM/8bc81aa0ff861097cb7886ea622a3d76 to your computer and use it in GitHub Desktop.
Nairuby code challenge(Divisible Sum Pairs)
#You are given an array of n integers, ar = [ ar[0], ar[1],...,ar[n-1]] , and a positive integer, k. Find and print the number of (i, j) pairs where i<j and ar[i]+ ar[j] is divisible by k.
def divisibleSumPairs(ar, k)
ar.combination(2).to_a.select{ |a| (a.index(a[0]) < a.index(a[1])) && ((a[0] + a[1]) % k == 0)}.count
end
array = [1, 2, 3, 4, 5, 6]
x = 5
p divisibleSumPairs(array, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment