We can solve it mathematically.
Let us call our solution s and p subset of s , where s[i] > 0 , that is, the set of numbers represented (any zero is a number or index that is not represented).
We can say that n = sum of all frequencies = sum p
Now let p' subset of p without s[0] , which are the frequencies of only numbers greater than zero.
It is clear that sum p' = sum p - s[0] = length p , which is simply the number of numbers in s greater than zero.
Remember that length p = length p' + 1 . Now, if length p > 4 , we know that sum p' > 4 , and we are left with a section of length m ( p' ), which should be added to m+1 , where m > 3 . The only way to do this is (m-1) 1 and one 2, for example, [1,1,1,2] in the case m=4 (by definition there are no zeros in p' ). Such a section may not make sense as a solution to our problem, and therefore we see that p or a subset of numbers greater than zero in our solution should have less than 5 elements.
Now we can solve for specific cases:
Each solution must have s[0] > 0 , since zero in the zero column will invalidate the solution.
length p = 1 is only possible if s[0] can simultaneously be zero and greater than zero.
length p = 2 means p' = [2] , and therefore there are two zeros and two 2, s=[2,0,2,0]
length p = 3 means p' = [1,2] . Since we know that there is only one more s[i] , which s[0] > 0 , 2 in p' must either refer to itself, and in this case we have s=[2,1,2,0,0] ; or to two 1 and therefore s=[1,2,1,0]
length p = 4, p' = [2,1,1] . In this case, 2 can refer only to two, and we must take s[0] > 2 , which also means sum p >= (3+2+1+1 = 7) . This is the final / general case that user1125600 found: s[1]=2, s[2]=1 . The last 1 refers to s[0] , so its index is s[0] . Recalling that sum p - s[0] = length p , we get s[0] = n - 4 , and the solution for p = 4, n> 6: s=[n - 4,2,1...1,0,0,0]