# Maximal number of edges in a graph with $n$ vertices and $p$ components

A graph with $$n$$ vertices and $$p$$ components has at least $$n-p$$ edges. Can I give an upper bound for the number of edges of such a graph? If so, how to prove it?

My attempt:

For each component, we have that the number of edges $$m_i$$ is given by $$m_i \le \binom{n_i}{2}$$, with $$n_i$$ the number of vertices within a component. So $$m \le \sum_{i=1}^p \binom{n_i}{2} \le p\binom{n}{2}$$.

Thanks.

The maximum occurs when $$p-1$$ of the components are singletons, so the number of edges is $$\binom{n-p+1}{2}$$.