Problem in the Concept

Why don’t we do backtracking in this question to undo the swaps that we did in past?
1
Here’s the link to my code: https://ide.codingblocks.com/s/337149