Elixir Distinct
defmodule Distinct do
def distinct(a), do: a |> Enum.uniq() |> length()
end
This counts unique values by tracking what has already been seen.
Elixir Dominator
defmodule Dominator do
def dominator(a) do
{size, value, index} =
a
|> Enum.with_index()
|> Enum.reduce({0, nil, nil}, fn {v, k}, {size, value, index} ->
cond do
size == 0 -> {1, v, k}
value != v -> {size - 1, value, index}
true -> {size + 1, value, index}
end
end)
candidate = if size > 0, do: value, else: -1
count = Enum.count(a, &(&1 == candidate))
if count > div(length(a), 2), do: index, else: -1
end
end
This finds a value that appears in more than half of the array, then returns one valid index for it.
Elixir Equi Leader
defmodule EquiLeader do
def equi_leader(a) do
{leader_size, value} =
Enum.reduce(a, {0, nil}, fn v, {size, value} ->
cond do
size == 0 -> {1, v}
value != v -> {size - 1, value}
true -> {size + 1, value}
end
end)
count = length(a)
candidate = if leader_size > 0, do: value, else: -1
leader_count = Enum.count(a, &(&1 == candidate))
leader = if leader_count > div(count, 2), do: candidate, else: -1
{equi_leaders, _l_leader_count} =
a
|> Enum.with_index()
|> Enum.reduce({0, 0}, fn {v, k}, {equi_leaders, l_leader_count} ->
left_half = div(k + 1, 2)
right_half = div(count - k - 1, 2)
l_leader_count = if v == leader, do: l_leader_count + 1, else: l_leader_count
r_leader_count = leader_count - l_leader_count
equi_leaders =
if l_leader_count > left_half and r_leader_count > right_half do
equi_leaders + 1
else
equi_leaders
end
{equi_leaders, l_leader_count}
end)
equi_leaders
end
end
This keeps leader counts on both sides of the split and counts positions where the same leader survives in each half.
Elixir Fib Frog
defmodule FibFrog do
def fib_frog(a) do
size = length(a)
jumps = size |> build_fib([1, 0]) |> Enum.reverse()
a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)
bfs([{-1, 0}], MapSet.new(), jumps, a_map, size)
end
defp build_fib(size, [last | _] = acc) when last > size do
acc |> Enum.reverse() |> Enum.drop(2)
end
defp build_fib(size, [a, b | _] = acc), do: build_fib(size, [a + b | acc])
defp bfs([], _visited, _jumps, _a_map, _size), do: -1
defp bfs([{idx, jmp} | rest], visited, jumps, a_map, size) do
case try_jumps(jumps, idx, jmp, size, a_map, visited) do
{:found, result} -> result
{:continue, new_paths, visited} -> bfs(rest ++ new_paths, visited, jumps, a_map, size)
end
end
defp try_jumps(jumps, idx, jmp, size, a_map, visited) do
Enum.reduce_while(jumps, {:continue, [], visited}, fn f, {:continue, paths, visited} ->
next_idx = idx + f
cond do
next_idx == size ->
{:halt, {:found, jmp + 1}}
next_idx > size or MapSet.member?(visited, next_idx) or Map.get(a_map, next_idx) == 0 ->
{:cont, {:continue, paths, visited}}
true ->
{:cont, {:continue, paths ++ [{next_idx, jmp + 1}], MapSet.put(visited, next_idx)}}
end
end)
end
end
This precomputes Fibonacci jumps, then uses a breadth-first search to find the shortest valid path across the river.
Elixir Fish
defmodule Fish do
def fish(a, b) do
size = length(a)
{_stack, dead} =
a
|> Enum.zip(b)
|> Enum.reduce({[], 0}, fn {size_i, dir_i}, {stack, dead} ->
if dir_i == 1 do
{[size_i | stack], dead}
else
fight(size_i, stack, dead)
end
end)
size - dead
end
defp fight(_size_i, [], dead), do: {[], dead}
defp fight(size_i, [top | rest] = stack, dead) do
if size_i > top do
fight(size_i, rest, dead + 1)
else
{stack, dead + 1}
end
end
end
This uses a stack for downstream fish and resolves fights only when opposite directions meet.
Elixir Flags
defmodule Flags do
def flags(a) do
size = length(a)
a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)
peaks =
for i <- 1..(size - 1), into: %{} do
prev = Map.get(a_map, i - 1)
cur = Map.get(a_map, i)
nxt = Map.get(a_map, i + 1, 0)
{i, prev < cur and cur > nxt}
end
next_map = build_next(size, peaks)
search(1, size, next_map, 0)
end
defp build_next(size, peaks) do
Enum.reduce((size - 2)..0//-1, %{size - 1 => -1}, fn i, next_map ->
value = if Map.get(peaks, i, false), do: i, else: Map.get(next_map, i + 1)
Map.put(next_map, i, value)
end)
end
defp search(i, size, next_map, result) when i * (i - 1) <= size do
num = walk(0, i, 0, size, next_map)
search(i + 1, size, next_map, max(result, num))
end
defp search(_i, _size, _next_map, result), do: result
defp walk(pos, step, num, size, next_map) when pos < size and num < step do
next_pos = Map.get(next_map, pos)
if next_pos == -1 do
num
else
walk(next_pos + step, step, num + 1, size, next_map)
end
end
defp walk(_pos, _step, num, _size, _next_map), do: num
end
This finds all peaks first, then checks how many flags can be placed while keeping the required distance.
Elixir Frog Jmp
defmodule FrogJmp do
def frog_jmp(x, y, d), do: ceil((y - x) / d)
end
This computes the jump count with math instead of simulation, which is the cleanest way to solve it.
Elixir Frog River One
defmodule FrogRiverOne do
def frog_river_one(x, a) do
a
|> Enum.with_index()
|> Enum.reduce_while(MapSet.new(), fn {leaf, k}, seen ->
seen =
if leaf <= x and not MapSet.member?(seen, leaf) do
MapSet.put(seen, leaf)
else
seen
end
if MapSet.size(seen) == x, do: {:halt, k}, else: {:cont, seen}
end)
|> case do
k when is_integer(k) -> k
_ -> -1
end
end
end
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Elixir Genomic Range Query
defmodule GenomicRangeQuery do
def genomic_range_query(s, p, q) do
p
|> Enum.zip(q)
|> Enum.map(fn {pi, qi} ->
substr = String.slice(s, pi, qi - pi + 1)
cond do
String.contains?(substr, "A") -> 1
String.contains?(substr, "C") -> 2
String.contains?(substr, "G") -> 3
true -> 4
end
end)
end
end
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
Elixir Is Ipv 4 Adress
defmodule IsIpv4Address do
def is_ipv4_address(input_string) do
parts = String.split(input_string, ".")
length(parts) == 4 and Enum.all?(parts, &valid_octet?/1)
end
defp valid_octet?(v) do
case Integer.parse(v) do
{n, ""} -> n >= 0 and n <= 255 and Integer.to_string(n) == v
_ -> false
end
end
end
This splits the string by dots and validates each part as a normal IPv4 octet.