Let number in A=x and in B=y. Let m =number of girls moved A to B. That increased proportion in B from 1/5 to 1/2. So increased it by 3/10 Hence m=3/10y.
m boys moved from B to A. Before proportion of boys was 1/8. Now it is 1/2. That is an increase of 3/8 Hence m=3/8x But 2m=24 so m=12
Hence 12=3/10y so y=40. 12=3/8x so x=32. So there were 32 in A and 40 in B.