Valency of an atom is the number of electrons that are required to be added or removed from the outermost shell of an atom to complete its octate.
Alumunium has an atomic number (Z) of 13
So, its electronic configuration is [Ne] 3s2 3p1
Hence, it needs to lose 3 electrons to attain stable configuration, hence its valency is three.
Oxygen has an atomic number (Z) of 18
So, its electronic configuration is [He]2s23p1.
therefore, it needs to gain more electrons to attain stable ocate. Hence, its valency is 2