Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 3f25ea8

Browse files
authored
W1/6 - Divisible Sum Pairs (#151)
Penultimate challenge to finalises #117 and closes #121
1 parent 0b7c774 commit 3f25ea8

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

w1/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Count the number of (i, j) index pairs such that i < j and the sum of arr[i] + a
6262

6363
| Python | Elixir | TypeScript | Go |
6464
|--------|--------|------------|----|
65-
| | |||
65+
|| |||
6666

6767

6868

w1/divisible_pairs.ex

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
defmodule HackerRank.W1.DivisibleSumPairs do
2+
@moduledoc """
3+
Divisible Sum Pairs - Challenge 6, Week 1.
4+
<https://www.hackerrank.com/challenges/three-month-preparation-kit-divisible-sum-pairs/problem>
5+
"""
6+
7+
@doc """
8+
Count the number of (i, j) index pairs such that i < j and the sum of arr[i] + arr[j] is divisible by a given integer k.
9+
10+
## Parameters
11+
12+
- _n (integer): The count of items in the supplied array (unused)
13+
- k (integer): the integer to be used as the divisor
14+
- ar (list[integers]): the array of integers to work on
15+
16+
Returns:
17+
- integer: the number of pairs that are divisible by k
18+
19+
Example:
20+
iex> DivisibleSumPairs.challenge(6, 5, [1, 3, 2, 6, 1, 2])
21+
2
22+
"""
23+
24+
@spec challenge(integer(), integer(), list(integer())) :: integer()
25+
def challenge(_n, k, arr) do
26+
# This is a recursy' thing
27+
count_pairs(k, arr, %{}, 0)
28+
end
29+
30+
# Tail recursive
31+
defp count_pairs(_k, [],_fmap, count), do: count
32+
defp count_pairs(k, arr, fmap, count) do
33+
# get the value
34+
[value | rest] = arr
35+
36+
# get the mod
37+
mod = rem(value, k)
38+
39+
# get the complement or zero if mod is zero
40+
comp = if mod == 0, do: 0, else: k - mod
41+
42+
# increment count by the frequency of the complement
43+
count =
44+
case Map.get(fmap, comp) do
45+
nil -> count
46+
hit -> count + hit
47+
end
48+
49+
# update the frequency map
50+
fmap = Map.update(fmap, mod, 1, &(&1 + 1))
51+
52+
# now go again
53+
count_pairs(k, rest, fmap, count)
54+
55+
end
56+
end

w1/divisible_pairs_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule HackerRank.W1.DivisibleSumPairsTests do
2+
use ExUnit.Case
3+
alias HackerRank.Test.Helper
4+
alias HackerRank.W1.DivisibleSumPairs
5+
doctest HackerRank.W1.DivisibleSumPairs
6+
7+
8+
@pattern "w1/tc/6_*txt"
9+
10+
test "run the tests" do
11+
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
12+
|> Enum.each(fn [[n, k, arr], expected] ->
13+
assert DivisibleSumPairs.challenge(n, k, arr) == expected
14+
end)
15+
end
16+
17+
def parse_args(raw) do
18+
# convert raw from a string ints seperated by spaces
19+
[[n, k], arr] =
20+
raw
21+
|> Enum.map(&String.split/1)
22+
|> Enum.map(fn args -> Enum.map(args, &String.to_integer/1) end)
23+
[n, k, arr]
24+
end
25+
26+
def parse_expected(raw) do
27+
Enum.map(raw, &String.to_integer/1) |> List.first
28+
end
29+
30+
end

0 commit comments

Comments
 (0)