Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion w1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Count the number of (i, j) index pairs such that i < j and the sum of arr[i] + a

| Python | Elixir | TypeScript | Go |
|--------|--------|------------|----|
| ✅ | | ➖ | ➖ |
| ✅ | | ➖ | ➖ |


Expand Down
56 changes: 56 additions & 0 deletions w1/divisible_pairs.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule HackerRank.W1.DivisibleSumPairs do
@moduledoc """
Divisible Sum Pairs - Challenge 6, Week 1.
<https://www.hackerrank.com/challenges/three-month-preparation-kit-divisible-sum-pairs/problem>
"""

@doc """
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.

## Parameters

- _n (integer): The count of items in the supplied array (unused)
- k (integer): the integer to be used as the divisor
- ar (list[integers]): the array of integers to work on

Returns:
- integer: the number of pairs that are divisible by k

Example:
iex> DivisibleSumPairs.challenge(6, 5, [1, 3, 2, 6, 1, 2])
2
"""

@spec challenge(integer(), integer(), list[integer()]) :: integer()
def challenge(_n, k, arr) do
# This is a recursy' thing
count_pairs(k, arr, %{}, 0)
end

# Tail recursive
defp count_pairs(_k, [],_fmap, count), do: count
defp count_pairs(k, arr, fmap, count) do
# get the value
[value | rest] = arr

# get the mod
mod = rem(value, k)

# get the complement or zero if mod is zero
comp = if mod == 0, do: 0, else: k - mod

# increment count by the frequency of the complement
count =
case Map.get(fmap, comp) do
nil -> count
hit -> count + hit
end

# update the frequency map
fmap = Map.update(fmap, mod, 1, &(&1 + 1))

# now go again
count_pairs(k, rest, fmap, count)

end
end
30 changes: 30 additions & 0 deletions w1/divisible_pairs_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule HackerRank.W1.DivisibleSumPairsTests do
use ExUnit.Case
alias HackerRank.Test.Helper
alias HackerRank.W1.DivisibleSumPairs
doctest HackerRank.W1.DivisibleSumPairs


@pattern "w1/tc/6_*txt"

test "run the tests" do
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
|> Enum.each(fn [[n, k, arr], expected] ->
assert DivisibleSumPairs.challenge(n, k, arr) == expected
end)
end

def parse_args(raw) do
# convert raw from a string ints seperated by spaces
[[n, k], arr] =
raw
|> Enum.map(&String.split/1)
|> Enum.map(fn args -> Enum.map(args, &String.to_integer/1) end)
[n, k, arr]
end

def parse_expected(raw) do
Enum.map(raw, &String.to_integer/1) |> List.first
end

end
Loading