Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
We can group the values level by level and solve each group independently.
Do BFS to group the value level by level.
Find the minimum number of swaps to sort the array of each level.
While iterating over the array, check the current element, and if not in the correct index, replace that element with the index of the element which should have come.