(d) If $h$ is the common height when they are connected, by conservation of mass
$\rho A_{1}h_{1} + \rho A_{2}h_{2} = \rho h(A_{1} + A_{2})$
$h = (h_{1} + h_{2})/2$ [As $A_{1} = A_{2} = A$ given]
As $(h_{1}/2)$ and $(h_{2}/2)$ are heights of initial centre of gravity of liquid in two vessels, the initial potential energy of the system
$U_{i} = \left( h_{1}A\rho \right)g\frac{h_{1}}{2} + \left( h_{2}A\rho \right)g\frac{h_{2}}{2} = \rho gA\frac{\left( h_{1}^{2} + h_{2}^{2} \right)}{2}$ …(i)
When vessels are connected the height of centre of gravity of liquid in each vessel will be $h/2$
$i.e.$ $\left( \frac{\left( h_{1} + h_{2} \right)}{4} \right)\ \ \left\lbrack as\ h = (h_{1} + h_{2})/2 \right\rbrack$
Final potential energy of the system
$U_{F} = \left\lbrack \frac{\left( h_{1} + h_{2} \right)}{2}A\rho \right\rbrack g\left( \frac{h_{1} + h_{2}}{4} \right)$
$= A\rho g\left\lbrack \frac{\left( h_{1} + h_{2} \right)^{2}}{4} \right\rbrack$ …(ii)
Work done by gravity
$W = U_{i} - U_{f} = \frac{1}{4}\rho gA\left\lbrack 2\left( h_{1}^{2} + h_{2}^{2} \right) - \left( h_{1} + h_{2} \right)^{2} \right\rbrack$
$= \frac{1}{4}\rho gA\left( h_{1} - h_{2} \right)^{2}$