Skip to content

Instantly share code, notes, and snippets.

@filipevarjao
Last active November 20, 2016 13:49
Show Gist options
  • Select an option

  • Save filipevarjao/72821d0c4e8dada85184629678f0c6f7 to your computer and use it in GitHub Desktop.

Select an option

Save filipevarjao/72821d0c4e8dada85184629678f0c6f7 to your computer and use it in GitHub Desktop.
Equilibrium index of a array
-module(equi).
-export([start/1]).
-spec start([any()]) -> integer().
start(Array) ->
check_equi([], Array, 0).
-spec check_equi([any()], [any()], non_neg_integer()) -> integer().
check_equi(_, [], _) -> -1;
check_equi(PreList, [Head|Tail], EquIndice) ->
case lists:sum(PreList) == lists:sum(Tail) of
true ->
EquIndice;
false ->
check_equi([Head|PreList], Tail, EquIndice + 1)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment