Erlang Max Counters
-module(max_counters).
-export([max_counters/2]).

max_counters(N, A) ->
    Condition = N + 1,
    {CountersMap, _MaxCounter, LastUpdate} = lists:foldl(fun(V, {Map, MaxC, Last}) ->
        case V of
            Condition ->
                {Map, MaxC, MaxC};
            _ when V =< N ->
                Index = V - 1,
                NewVal = max(maps:get(Index, Map, 0), Last) + 1,
                {maps:put(Index, NewVal, Map), max(MaxC, NewVal), Last};
            _ ->
                {Map, MaxC, Last}
        end
    end, {#{}, 0, 0}, A),
    [max(maps:get(I, CountersMap, 0), LastUpdate) || I <- lists:seq(0, N - 1)].

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