General approach for problems like "If a coin is tossed $n$ times, what is the probability that heads and tails appear $x$ and $y$ times"? If a fair coin is tossed four times. What is the probability that two heads and two tails will result?


I was solving the question above, since the sample space was small, I was able to list down all possible combinations, but what if the number of coins was let's say 20, how would one solve it?
I think I've found a solution to my own question-
The sample space $n(S)=2^4$ combinations.
let A be the event of occurrence of heads $x$ times.
Therefore, the combinations in which heads may appear $x$ times is $\dbinom{4}{x}$.
So, the probability $P(A)$ that heads will appear $x$ times is -
$P(A)=\frac{\dbinom{4}{x}}{\Large n(S)}$
First thing is to note that any two heads two tails flip (e.g. HHTT, HTTH) because of independence of flips has probability of $\frac{1}{2^4}$ of happening. Now all we must do is count how many of these type of events there are. Well if we want 2 out of the 4 flips to be heads (thus other two must be tails), which is $4 \choose 2$ now putting this together we have that. The probability of getting two heads out of two flips is ${4 \choose 2} \frac{1}{2^4}$. 

Now extending this to $n$ flips. The probability of seeing $x$ heads we have is 
$${ n\choose x}\left(\frac{1}{2}\right)^n$$