Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Untitled notebook

Find_pair_and_last_positio.livemd

Untitled notebook

Section

defmodule Solution do
  @spec search_range(nums :: [integer], target :: integer) :: [integer]
  def search_range(nums, target) do
    get_indices(nums, target, 0, [-1, -1])
  end

  def get_indices([], _, _, indices), do: indices

  def get_indices([h | nums], target, count, [x, _] = indices) do
    cond do
      h == target ->
        get_indices(nums, target, count + 1, [if(x == -1, do: count, else: x), count])

      true ->
        get_indices(nums, target, count + 1, indices)
    end
  end
end

Solution.search_range([5, 7, 7, 8, 8, 10], 5)