If you’re cramping while exercising you need to take potassium. If you’re cramping outside of exercising, even if it’s an exercise type move that triggers it, you need to take magnesium.
For example, I don’t get cramps while running. But in the past when trying to stretch while sleeping, or trying to throw a football with my whole body into it, I triggered a cramp. That’s from lack of magnesium. I’ve never experienced lack of potassium as cramps before. But it’s good to know when one helps vs the other.
As already said, electrolytes. For getting salt and potassium I like taking Lite Salt in my drinks or on food. Careful of magnesium pills that can also give you loose stools; I’ve been taking Magnesium Bisglycinate Chelate to good effect, also helps with fasting.