Kindly help me understand this problem from hackerrank

Problem link :

Kindly give me an explanation for the solution of above problem.

Thank You

If any person is more than two positions away from its orig position then too chaotic.

Since any swap changes the total number of position changes by 2, the total number must be even.

You can simply loop, starting at position 1. Find person number 1. (its on position 2 or 3)
Swap that persons position with the one left of it once or twice.
Repeat with second… until end of list.

1 Like

You can use a bruteforce approach as well as a more efficient linear one.

1 Like