X is made up of only magnesium, aluminium and oxygen. The valency of Magnesium is +2 and that of aluminium is +3 both are positive ions. The valency of oxygen is -2 and it is negative ion.
If we take one magnesium ion and two aluminium ion (one aluminium ion's +3 charge cannot be compensated by oxygen -2 charge). The total positive charge is
1XMg + 2xAl = 1x +2 + 2x+3= +8
For this we want of four oxygen atoms = 4x-2=-8
So the formula of the compound is MgAl2O4