Can anyone please explain the solution of PJ18E?
Please use an appropriate title and always provide a link to the question.
1 Like
The idea is to not move elements you do not need to. Clearly you do not need to move the smallest element since you can move everything else to the end. Similarly, if the second smallest element occurs at any any position after the smallest, you do not need to move that either. Moving everything else will make sure the smallest and second smallest are consecutive. This logic can be extended to the rest of the elements also.
So the elements which already occur in the required sorted order, beginning from the smallest, will be left untouched and all other elements need to be moved to the end. For reference, this seems like an easy-to-read solution: 19658647