Well, that is simple.
A Carbon(C) makes 4 bonds, a Hydrogen(H) or a halogen (X) makes 1 bond and Nitrogen(N) makes three bonds. So in a molecule with C Carbons, H Hydrogens, X Halogens and N Nitrogens, we will have $\small\ce{4*C +3*N + X +H}\ \ \ $ "bonding capacity".
Assuming there are no rings, no double/triple bonds, we must have $\small\ce{C +H + X +N -1}$ bonds in between these atoms [think like how many dashes you can draw between $n$ $\ce{O}$'s: $\ce{O-O-O}$ two dashes for three $\ce{O}$'s apparently].
Since each bond uses one "bonding capacity" from each of the two atoms its binding, $\small\ce{C +H +X +N -1}$ bonds are using $\small\ce{2*(C +H +X +N-1)}$ bonding capacity. So the remaining capacity is:
$$\ce{(4*C +3*N +X +H)\ -\ 2*(C +H +X +N - 1)\ =\ 2*C-H-X +N +2}$$
If this is larger than zero, it means our molecule is unsaturated, i.e. still has a capacity to make bonds. And our assumption that "there are no rings, no double/triple bonds" is wrong. This bonding capacity is used in forming new bonds either in rings or in pi-bonds. Since each bond uses two capacity one from each side we should have $\small\ce{C-H/2-X/2+N/2+1}$ "unsaturated" bonds.