Replace by MEX
HardRating 1900CodeforcesConstructive Algorithms
Approach hintTry it first, then revealHide
Aim for the final array 0, 1, ..., n-1. If the current MEX m is below n, write it at index m; if it equals n, write it into any index that is still wrong. Each index is fixed within two operations.