As per the given diagram,
At maximum compression reduced mass of system \(= \left( \frac{mM}{M+ m}\right)\)
Initial relative velocity of approach = (v1 − v2)
Hence, by conservation of energy,
Potential Energy of the spring = Kinetic Energy of the blocks
\(\frac 12kx_{max}^2 = \frac 12\left(\frac{mM}{m + M}\right)\left(v_1 - v_2\right)^2\)
Therefore,
\(x_{max} = \sqrt{\frac{mM}{k(M+ m)}}(v_1 - v_2)^2\)