Heads Up, Heads Down

You are blindfolded and on the table, in front of you, 50 coins are placed. You are told that X of them are heads up and the rest are heads down. Then you are asked to separate the coins into two groups and optionally flip some of them so that the number of heads in both groups becomes the same. How can you do this?

Separate the coins in one group of X coins and in another group of 50-X coins, then flip every coin in the first group. If in the first group there were Y heads up initially, then after flipping there will be X-Y – exactly the number of heads up in the second group.

We do not know where this puzzle originated from. If you have any information, please let us know via email.