Out of all bit strings of length $N$, we need to count how many of them are there in which all the ones are present in a window of length $K$.
For this, my initial thought was:
The starting point of the window can be placed at $(N - K)$ positions. In each window, each bit can either be $1$ or $0$. The number of bit strings would then be $(N - K) * 2^k$.
However, there is over-counting involved in this method.
How do I solve this problem?
There are $N+1$ bit strings of length $N$ having zero or one $1$s.
If such a bit string has $\geq2$ ones it has a first $1$ and a last $1$, and anything in between. Let $r\geq2$ be the length of the window determined by the first and the last $1$. There are $N-r+1$ positions of such a window along the string. The total number $n$ of admissible strings is therefore given by $$n=N+1+\sum_{r=2}^K(N-r+1)2^{r-2}\ .$$ When you compute the sum you'll obtain a result so simple that it makes you think whether there is not an easier solution.