Solution (source code)

= Solution

Define the <Lagrange interpolation polynomials>
$$
L_j(x)=\prod_{\ell\ne j}\frac{x-x_\ell}{x_j-x_\ell},\qquad\boxed{A_j=\int_{-1}^1L_j(x)\,dx.}
$$
Distinct <interpolation nodes> ensure every denominator is nonzero and $L_j(x_i)=\delta_{ij}$. For a <polynomial> of degree at most $n-1$, the difference $P(x)-\sum_jP(x_j)L_j(x)$ has degree at most $n-1$ and vanishes at $n$ distinct points, so it is zero. Integrating this <Lagrange interpolation> identity proves the required exact quadrature formula. This also covers $n=1$, when the empty product is one and its weight is two.