Elixir Nesting
defmodule Nesting do
def nesting(""), do: 1
def nesting(s) do
result =
s
|> String.graphemes()
|> Enum.reduce_while([], fn ch, stack ->
case ch do
")" ->
case stack do
["(" | rest] -> {:cont, rest}
_ -> {:halt, :fail}
end
other ->
{:cont, [other | stack]}
end
end)
if result == [], do: 1, else: 0
end
end
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Elixir Number Of Disc Intersections
defmodule NumberOfDiscIntersections do
def number_of_disc_intersections(a) do
c = length(a)
{starts, ends} =
a
|> Enum.with_index()
|> Enum.reduce({%{}, %{}}, fn {v, k}, {starts, ends} ->
start_key = max(0, k - v)
end_key = min(c - 1, k + v)
{Map.update(starts, start_key, 1, &(&1 + 1)), Map.update(ends, end_key, 1, &(&1 + 1))}
end)
0..(c - 1)
|> Enum.reduce_while({0, 0}, fn k, {sum, active} ->
s = Map.get(starts, k, 0)
e = Map.get(ends, k, 0)
sum = sum + active * s + div(s * (s - 1), 2)
if sum > 10_000_000 do
{:halt, {:found, -1}}
else
{:cont, {sum, active + s - e}}
end
end)
|> case do
{:found, -1} -> -1
{sum, _active} -> sum
end
end
end
This sorts disc start and end points and counts active overlaps without comparing every pair directly.
Elixir Odd Occurrences In Array
defmodule OddOccurrencesInArray do
def odd_occurrences_in_array(a) do
a
|> Enum.reduce(%{}, fn v, count ->
if Map.has_key?(count, v), do: Map.delete(count, v), else: Map.put(count, v, 1)
end)
|> Map.keys()
|> List.first()
end
end
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Elixir Palindrome Rearranging
defmodule PalindromeRearranging do
def palindrome_rearranging(input_string) do
input_string
|> String.graphemes()
|> Enum.frequencies()
|> Map.values()
|> Enum.count(&(rem(&1, 2) != 0))
|> Kernel.<=(1)
end
end
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Elixir Passing Cars
defmodule PassingCars do
def passing_cars(a) do
a
|> Enum.reduce_while({0, 0}, fn i, {passing, multiply} ->
cond do
i == 0 ->
{:cont, {passing, multiply + 1}}
multiply > 0 ->
passing = passing + multiply
if passing > 1_000_000_000 do
{:halt, {:found, -1}}
else
{:cont, {passing, multiply}}
end
true ->
{:cont, {passing, multiply}}
end
end)
|> case do
{:found, -1} -> -1
{passing, _multiply} -> passing
end
end
end
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Elixir Peaks
defmodule Peaks do
def peaks(a) when length(a) <= 2, do: 0
def peaks(a) do
n = length(a)
a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)
{sum, dist, last_peak} = scan_peaks(a_map, n)
total_peaks = Map.get(sum, n - 2, 0)
sum = Map.put(sum, n - 1, total_peaks)
if total_peaks == 0 do
0
else
dist = max(dist, n - last_peak)
case find_divisor(div(dist, 2) + 1, dist, n, sum) do
{:ok, groups} -> groups
:none -> div(n, find_valid_divisor(dist, n))
end
end
end
defp scan_peaks(a_map, n) do
Enum.reduce(1..(n - 2), {%{0 => 0}, 0, -1}, fn i, {sum, dist, last} ->
prev_sum = Map.get(sum, i - 1)
is_peak =
Map.get(a_map, i) > Map.get(a_map, i - 1) and
Map.get(a_map, i) > Map.get(a_map, i + 1)
if is_peak do
{Map.put(sum, i, prev_sum + 1), max(dist, i - last), i}
else
{Map.put(sum, i, prev_sum), dist, last}
end
end)
end
defp find_divisor(i, dist, _n, _sum) when i >= dist, do: :none
defp find_divisor(i, dist, n, sum) do
if rem(n, i) == 0 do
case walk_groups(i, i, n, sum, 0) do
{:ok, last_j} when last_j > n -> {:ok, div(n, i)}
_ -> find_divisor(i + 1, dist, n, sum)
end
else
find_divisor(i + 1, dist, n, sum)
end
end
defp walk_groups(j, _step, n, _sum, _last) when j > n, do: {:ok, j}
defp walk_groups(j, step, n, sum, last) do
current = Map.get(sum, j - 1)
if current <= last do
{:ok, j}
else
walk_groups(j + step, step, n, sum, current)
end
end
defp find_valid_divisor(last, n) when rem(n, last) == 0, do: last
defp find_valid_divisor(last, n), do: find_valid_divisor(last + 1, n)
end
This finds the peak positions, then tests how many equal blocks can each contain at least one peak.
Elixir Perm Check
defmodule PermCheck do
def perm_check(a) do
sorted = Enum.sort(a)
size = length(sorted)
ok =
0..(size - 2)//1
|> Enum.all?(fn k -> Enum.at(sorted, k) == k + 1 end)
if ok, do: 1, else: 0
end
end
This validates that every value from 1 to N appears exactly once.
Elixir Perm Missing Element
defmodule PermMissingElement do
def perm_missing_element(a) do
sorted = Enum.sort(a)
sorted
|> Enum.with_index()
|> Enum.find_value(fn {v, k} -> if v != k + 1, do: k + 1 end)
|> Kernel.||(length(sorted) + 1)
end
end
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Elixir Plagiarism Check
defmodule PlagiarismCheck do
def plagiarism_check(code1, code2) do
c1 = Enum.join(code1, " ")
c2 = Enum.join(code2, " ")
if c1 == c2 do
false
else
words1 = ~r/\w+/ |> Regex.scan(c1) |> List.flatten()
words2 = ~r/\w+/ |> Regex.scan(c2) |> List.flatten()
candidates =
words1
|> Enum.zip(words2)
|> Enum.reduce(%{}, fn {w1, w2}, acc ->
if w1 != w2 and not numeric?(w1), do: Map.put(acc, w1, w2), else: acc
end)
c1_placeheld =
Enum.reduce(candidates, c1, fn {orig, _repl}, acc ->
replace_word(acc, orig, "PLACEHOLDER" <> orig)
end)
c1_final =
Enum.reduce(candidates, c1_placeheld, fn {orig, repl}, acc ->
replace_word(acc, "PLACEHOLDER" <> orig, repl)
end)
c1_final == c2
end
end
defp numeric?(w) do
case Integer.parse(w) do
{_n, ""} -> true
_ -> false
end
end
defp replace_word(text, target, replacement) do
escaped = Regex.escape(target)
text
|> String.replace(~r/(\W)#{escaped}(\W)/, "\\1#{replacement}\\2")
|> String.replace(~r/(\W)#{escaped}/, "\\1#{replacement}")
end
end
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Elixir Shape Area
defmodule ShapeArea do
def shape_area(1), do: 1
def shape_area(n), do: shape_area(n - 1) + 4 * (n - 1)
end
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.