Elixir Ladder
defmodule Ladder do
  import Bitwise

  def ladder(a, b) do
    mod = (1 <<< Enum.max(b)) - 1
    limit = Enum.max(a)
    fib = build_fib(limit, mod)

    a
    |> Enum.zip(b)
    |> Enum.map(fn {ai, bi} -> Map.get(fib, ai + 1) &&& (1 <<< bi) - 1 end)
  end

  defp build_fib(limit, mod) do
    Enum.reduce(2..(limit + 1)//1, %{0 => 0, 1 => 1}, fn i, fib ->
      value = (Map.get(fib, i - 1) + Map.get(fib, i - 2)) &&& mod
      Map.put(fib, i, value)
    end)
  end
end

This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.

Elixir Largest String
defmodule LargestString do
  def largest_string(s) do
    s
    |> String.to_charlist()
    |> Enum.chunk_by(&(&1 in ~c"ab"))
    |> Enum.map(&sort_segment/1)
    |> List.flatten()
    |> List.to_string()
  end

  defp sort_segment([c | _] = segment) when c in ~c"ab" do
    Enum.sort(segment, :desc)
  end

  defp sort_segment(segment), do: segment
end

This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.

Elixir Max Counters
defmodule MaxCounters do
  def max_counters(n, a) do
    condition = n + 1

    {counters, _max_counter, last_update} =
      Enum.reduce(a, {%{}, 0, 0}, fn v, {counters, max_counter, last_update} ->
        cond do
          v <= n ->
            index = v - 1
            updated = max(Map.get(counters, index, 0), last_update) + 1
            {Map.put(counters, index, updated), max(max_counter, updated), last_update}

          v == condition ->
            {counters, max_counter, max_counter}

          true ->
            {counters, max_counter, last_update}
        end
      end)

    0..(n - 1)
    |> Enum.map(fn i -> max(Map.get(counters, i, 0), last_update) end)
  end
end

This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.

Elixir Max Double Slice Sum
defmodule MaxDoubleSliceSum do
  def max_double_slice_sum(a) when length(a) < 3, do: 0

  def max_double_slice_sum(a) do
    size = length(a)
    a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)

    p1 = build_p1(size, a_map)
    p2 = build_p2(size, a_map)

    1..(size - 2)
    |> Enum.map(fn i -> Map.get(p1, i) + Map.get(p2, i) end)
    |> Enum.max()
  end

  defp build_p1(size, a_map) do
    Enum.reduce(2..(size - 2)//1, %{1 => 0}, fn i, p1 ->
      value = max(0, Map.get(p1, i - 1) + Map.get(a_map, i - 1))
      Map.put(p1, i, value)
    end)
  end

  defp build_p2(size, a_map) do
    Enum.reduce(2..(size - 2)//1, %{size - 2 => 0}, fn i, p2 ->
      key = size - i - 1
      value = max(0, Map.get(p2, size - i) + Map.get(a_map, size - i))
      Map.put(p2, key, value)
    end)
  end
end

This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.

Elixir Max Product Of Three
defmodule MaxProductOfThree do
  def max_product_of_three(a) do
    sorted = Enum.sort(a)
    c = length(sorted)

    max(
      Enum.at(sorted, c - 1) * Enum.at(sorted, c - 2) * Enum.at(sorted, c - 3),
      Enum.at(sorted, 0) * Enum.at(sorted, 1) * Enum.at(sorted, c - 1)
    )
  end
end

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Elixir Max Profit
defmodule MaxProfit do
  def max_profit([first | _] = a) do
    {_min_price, profit} =
      Enum.reduce(a, {first, 0}, fn v, {min_price, profit} ->
        min_price = min(min_price, v)
        {min_price, max(profit, v - min_price)}
      end)

    profit
  end
end

This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.

Elixir Max Slice Sum
defmodule MaxSliceSum do
  def max_slice_sum([first | rest]) do
    {_tmp, max} =
      Enum.reduce(rest, {first, first}, fn v, {tmp, max} ->
        tmp = max(tmp + v, v)
        {tmp, max(max, tmp)}
      end)

    max
  end
end

This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.

Elixir Min Avg Two Slice
defmodule MinAvgTwoSlice do
  def min_avg_two_slice(a) do
    size = length(a)
    a_map = a |> Enum.with_index() |> Map.new(fn {v, i} -> {i, v} end)
    initial_avg = (Map.get(a_map, 0) + Map.get(a_map, 1)) / 2

    {idx, _min_avg} =
      Enum.reduce(0..(size - 2), {0, initial_avg}, fn i, {idx, min_avg} ->
        two = (Map.get(a_map, i) + Map.get(a_map, i + 1)) / 2

        cur =
          if Map.has_key?(a_map, i + 2) do
            three = (Map.get(a_map, i) + Map.get(a_map, i + 1) + Map.get(a_map, i + 2)) / 3
            min(two, three)
          else
            two
          end

        if cur < min_avg, do: {i, cur}, else: {idx, min_avg}
      end)

    idx
  end
end

This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.

Elixir Min Perimeter Rectangle
defmodule MinPerimeterRectangle do
  def min_perimeter_rectangle(n), do: loop(1, n, :infinity)

  defp loop(i, n, min) when i * i < n do
    candidate = if rem(n, i) == 0, do: 2 * (i + div(n, i)), else: nil

    min =
      cond do
        is_nil(candidate) -> min
        min == :infinity -> candidate
        candidate < min -> candidate
        true -> min
      end

    loop(i + 1, n, min)
  end

  defp loop(_i, _n, min), do: min
end

This searches factor pairs up to the square root and picks the pair with the smallest perimeter.

Elixir Missing Integer
defmodule MissingInteger do
  def missing_integer(a) do
    sorted = a |> Enum.uniq() |> Enum.sort()

    Enum.reduce_while(sorted, 1, fn v, min ->
      cond do
        v <= 0 -> {:cont, min}
        min != v -> {:halt, min}
        true -> {:cont, min + 1}
      end
    end)
  end
end

This records the positive numbers that exist, then returns the smallest positive value that is still missing.