Dear student,
According to hooke's law F=-Kx
Kinetic energy of mass m moving with velocity is 1/2m and is transferred to another mass M and force exerted on that is 1/2 mwhere x is compression in the spring
Using Hooke's law and this expression for force
compression comes out to be
With Regards
Tanvi Mahajan