[Haskell], 102 bytes
import Data.List
f(x:r)|length r<x=0>1|1<3=f.reverse.sort$[n|n<-pred<$>take x r,n>0]++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 = False -- if the rest list is shorter than x, return False
|otherwise = f . -- else call f recursively
reverse . sort $ -- with the descendingly sorted list ...
[n|n<-pred<$>take x r, n>0] -- of the first x elements of r subtracted by 1 if they are still greater 0 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/ [Try it online!]: https://tio.run/nexus/haskell#bYwxD4IwFIR3fsUbGDRUAqKJmpbJ0X/QdGhCq41SmkdjOvDfkaLoYr7l7t67G03rOvRwll7mF9P7RK/CCdfDQ9mrvwHSwIq6HEpaMZ2jeirsVd5PlZTbwdKNQ9XQtPbyriAAElsXIssa7Fy0iYbApu7YSmOBQdNBAgAOjfWQQisdaOBcEOAViWxnysgSfoJoD@/LjxguUvxbnge@X1EcyY7shRhf "Haskell – TIO Nexus"