In Sanskrit "a" is a prefix which changes the meaning of a word into its opposite. "vidya" means understanding and "avidya" means ignorance. This pattern is same in many Greek words also, like "atheism" vs. "theism", etc.
There are other prefixes also, like "an” and “nir” which denote the opposite of the word they are attached with.
Also, the prefix "vi" is added to a noun make the meaning opposite. E.g. yog योग means joining while viyog वियोग means separation. Again, "vi" prefix has another role of meaning more "specific" or "specialist" than the word attached to, like gyan ज्ञान is knowledge while vigyan विज्ञान is specific knowledge.
So there is no blanket rule that opposite can be made by only attaching "a" or "an" and it varies...