Your first approach is indeed correct. Here is an alternate (much more laboriously exhaustive) way of thinking about the problem:
Part I
You can reformulate the problem, by instead thinking about arranging two types of "tiles": (A) a box containing a ball, and (B) a box lacking a ball, followed by a box containing a ball.
For example, suppose $n=9$ and $k=6$. In this case, you must have $n-k=3$ empty boxes. Thus, you will have $n-k=3$ of "tile B". This leaves $n-2(n-k)=2k-n=3$ balls to be used as instances of "tile A".
To illustrate the idea: let $(1)$ denote "tile A", i.e. a box containing a ball, and let $(01)$ denote "tile B", i.e. a box lacking a ball followed by a box containing a ball. Here are a few arbitrary arrangements of these "tiles":
$$
(1)(1)(01)(1)(01)(01) = 1,1,0,1,1,0,1,0,1 \\
(01)(01)(1)(1)(1)(01) = 0,1,0,1,1,1,1,0,1 \\
(01)(1)(1)(01)(01)(1) = 0,1,1,1,0,1,0,1,1
$$
As you may observe from these examples, we have $(n-k)+(2k-n)=k$ total "tiles", out of which we must choose $n-k$ positions for the $(01)$s. The total number of ways we can do this is $\binom{k}{n-k}$.
Part II
At this point, we run into a problem: our two tiles $(1)$ and $(01)$ admit every way to produce a sequence with ends with a filled box, i.e. $1$... but no way to produce a sequence which ends with an empty box, i.e. $0$. For example, the following are valid ball arrangements with no two consecutive empty boxes, but there is no way to divide them up into $(1)$ and $(01)$ only:
$$
0,1,0,1,1,1,1,1,0 = (01)(01)(1)(1)(1)(1)0 \\
1,0,1,1,1,1,0,1,0 = (1)(01)(1)(1)(1)(01)0 \\
1,1,1,1,0,1,0,1,0 = (1)(1)(1)(1)(01)(01)0
$$
Thus, we must introduce a new tile: let $(0)$ denote "tile C". However, "tile C" comes with a couple of very specific restrictions: (i) it can only appear at most once in any tile set, and (ii) if it does appear, it must be the very last tile. These restrictions make it so that we can effectively ignore "tile C" when enumerating the sequences of boxes which end with an empty box.
For example: in the three sequences shown above which end in $0$, we can effectively just ignore the $0$ as it does not affect anything, and observe that from $k$ "tiles", we must choose $n-k-1$ positions for the $(01)$s.
Part III
In Part I, we showed that the number of combinations which end in $1$ is $\binom{k}{n-k}$. Subsequently, in Part II, we showed that the number of combinations which end in $0$ is $\binom{k}{n-k-1}$. "Combinations which end in $1$" and "combinations which end in $0$" are two mutually exclusive, collectively exhaustive sets, which together comprise the set of all possible combinations. Thus, the total number of possible combinations is simply the sum of the two results from Parts I & II:
$$
\binom{k}{n-k} + \binom{k}{n-k-1} \\
= \binom{k+1}{n-k}
$$
Note that this solution is valid only when $n/2 \le k \le n$; if not, then the answer is $0$. If $k < n/2$, then there are less than half as many balls as boxes, which makes it impossible to avoid two consecutive empty boxes occurring somewhere. And if $k > n$, then there are more balls than boxes, which by the pigeonhole principle implies that at least one box must have two or more balls.
Brute force check with Python 3.x
The following Python script does the following: for $1\le n \le 20$, and for $\lceil{n/2}\rceil \le k \le n$, (i) manually lists out all the distinct valid combinations, and counts them; and (ii) calculates the number of distinct valid combinations, by way of $\binom{k+1}{n-k}$. The output confirms that for each checked $(n,k)$, the values from (i) and (ii) agree.
from math import ceil, comb
def distinct_permutations(iterable):
iterable = sorted(iterable)
size = len(iterable)
while True:
yield tuple(iterable)
for i in range(size - 2, -1, -1):
if iterable[i] < iterable[i + 1]:
break
else:
return
for j in range(size - 1, i, -1):
if iterable[i] < iterable[j]:
break
iterable[i], iterable[j] = iterable[j], iterable[i]
iterable[i + 1 :] = iterable[: i - size : -1]
def test(n_max):
agree = []
for n in range(1,n_max+1):
k = ceil(n/2)
while k <= n:
boxes = '1'*k + '0'*(n-k)
all_combos = [''.join(c) for c in distinct_permutations(boxes)]
valid_combos = [c for c in all_combos if '00' not in c]
manual = len(valid_combos)
calc = comb(k + 1, n - k)
agree.append(manual==calc)
k += 1
return all(agree)
if __name__ == '__main__':
print(test(n_max=20))
Output:
True
Best Answer
Okay so using we know that the number of ways without restrictions is simply $$\binom{n+r-1}{r-1}$$ And using $n=20,r=4$ we get $$\binom{23}{3}$$Now if we put $19$ balls in any one box then we have $\binom{4}1$ ways of selecting the one box and then we also have to select the next box to hold the last ball which can be done in $3$ ways each time, so we get for the case of $19$ balls in one box, $12$ ways that should not be included.
Also for the case of $20$ balls in one box, we get $4$ ways of doing that, so you get in total $16$ cases to be subtracted from the original answer, which leaves us with option c) $$\binom{23} 3 -16$$