Initially, only block-A is moving with velocity 2ms.
Hence initial momentum =3×2=6kg−ms..................(1)
At time of maximum compression of spring, let the common velocity be v m/s
Hence final momentum =(3+2)×v=5×vkg−ms................(2)
By conservation of momentum, we equate (1) and (2) to get common velocity v=1.2ms
Initial kinetic energy =(12)×3×2×2=6Joules................(3)
Final kinetic energy =(12)×5×1.2×1.2=3.6Joules..............(4)
difference in kinetic energy is stored as potential energy in spring,
i.e., (12)kx2=(12)×480×x2=6−3.6=2.4Joules..........(5)
from (5), we get the maximum displacement, x=0.1m