When the spring is stretched through a distance d
The ratio of the distance moved by m1 to that of m2 is such that d1/ d2 = m2/ m1
This can be written as d1 /( d1 + d2) = m2/ ( m1 + m2)
d1 = m2d /( m1 + m2), Since x = d1+ d2
m1 again comes to rest when its distance moved is 2d1.
Hence, 2d1 = m2* 2d/ ( m1 + m2)
Similarly for the mass m2, distance moved is 2d2 = m1 * 2d / (m1 + m2)