When the tube is turned vertical, let the mercury be displaced by a distance x. The new lengths of the air columns will be (40−x) for the lower section and (40+x) for the upper section.
Applying Boyle's Law (P1V1=P2V2) for both air columns:
Upper air column: P1(40+x)=P0(40)⟹P1=40+x76×40
Lower air column: P2(40−x)=P0(40)⟹P2=40−x76×40
P2=P1+20
40−x76×40=40+x76×40+20
40−x152=40+x152+1
152[40−x1−40+x1]=1
152[1600−x2(40+x)−(40−x)]=1
152(1600−x22x)=1⟹304x=1600−x2
x2+304x−1600=0
x≈2−304+314.35=210.35≈5.18 cm