For mass m, the forces are given as
T−mg=ma ...... (1)
Similarly, for mass M, the forces are given as
Mg−T=Ma ...... (2)
From equation (1), we have
T=mg+ma ...... (3)
Substituting equation (3) in (2) we get
Mg−mg−ma=Ma
g(M−m)=a(M+m)
a=(M−mM+m)g
T=m×(M−mM+m)g+mg
T=Mm−m2M+mg+mg
T=g(Mm−m2M+m+m)
T=g(Mm−m2+m2+MmM+m)=g(2MmM+m)