From the free body diagram of the block of mass m1:
m1a1 = T ....(i)
From the free body diagram of the block of mass m2:
m2a2 = m2g - 2T ....(ii)
Substituting for T in the equation (ii), we have
m2a2 = m2g - 2m1a1 ...(iii)
When the blocks move, the total length of the string remains unchanged. Therefore, if the block of mass m1 moves towards right through a distance x, the block of mass m2 moves down through a distance x/2. Hence, the acceleration of the block of mass m2, must be one half as that of the block mass m2,
i.e., a2 = 1/2 a1, or a1 = 2a2
Therefore, the equation (iii) becomes
m2a2 = m2g - 2m2 x 2a2 ....(iv)
or, 4m1a2 + m2a2 = m2g
or, a2 = {m2g}/{4m1 + m2}
Also, a1 = 2a2 = {2m2g}/{4m1 + m2}