For the mass M

Net force on the system $Mg-mg+mg$

$=Mg$

Total mass being pulled $=M+2m$

Therefore $ a=\large\frac{Mg}{M+2m}$

We have $a < g$

There should be an upward force on M. so that its acceleration becomes less than g. Hence for any value of M spring is elongated