[Haskell], 102 98 bytes
import Data.List
f(x:r)|length r<x||x<0=0>1|1<3=f.reverse.sort$(pred<$>take x r)++drop x r
f x=1<3
Try it online! Usage: f [3,3,2,2,1,1]
, returns True
or False
. Assumes that the input contains no zeros and is sorted in descending order, as allowed in the challenge.
Explanation:
import Data.List -- import needed for sort
f (x:r) -- x is the first list element, r the rest list
|length r < x || x<0 = False -- if the rest list is shorter than x or x<0, return False
|otherwise = f . -- else call f recursively
reverse . sort $ -- with the descendingly sorted list ...
(pred<$>take x r) -- of the first x elements of r subtracted by 1 and ...
++ drop x r -- the rest of r
f [] = True -- if the list is empty, return True
Edit: This seems to follow the Havel-Hakimi mentioned in other answers, though I did not know of this algorithm when writing the answer. [Haskell]: https://www.haskell.org/