Problem 3431 · Medium

Minimum Unlocked Indices to Sort Nums

nums holds only 1, 2 and 3. Neighbours at i and i + 1 may swap when nums[i] is exactly one more than nums[i + 1] and index i is unlocked. Find the fewest indices to unlock so nums can reach sorted order, or -1 if no choice works.

Open the interactive visualisation