Assign values from largest to smallest. For each with , its report is equivalent to two requirements:
There must be exactly one zero report, at the maximum.
Represent the first requirement by precedence edges : process before . The second requirement enables once either boundary witness has been processed. Initially enable only the zero-report position.
Repeatedly choose any enabled position with no remaining mandatory predecessor, and assign it the largest unused value. Processing it removes its outgoing precedence edges and enables positions for which it is a boundary witness.
This greedy choice is safe. In any valid completion, move an eligible position to the front. It has no unprocessed mandatory predecessor and already has a larger witness. If moving it earlier made it a forbidden closer larger value for another position , the required edge would be an unprocessed predecessor, contradicting eligibility. Providing an earlier boundary witness only helps. Thus every eligible choice preserves the existence of a completion.
If no position is eligible, the first position of a supposed valid completion would need to be enabled and have no unprocessed predecessor, a contradiction. If all positions are processed, both requirements hold at every position, so the resulting permutation is valid.
The outgoing neighbors of are two intervals:
Build the initial predecessor counts using difference-array additions on these intervals. Maintain the counts in a lazy segment tree supporting range addition, a global minimum, and finding a position attaining zero.
Add a penalty to every disabled position. True predecessor counts are nonnegative, so disabled positions cannot be selected. When a position receives its first boundary witness, subtract . Store reverse witness lists; their total size is at most .
After selecting a position, replace its zero with a sentinel greater than , then subtract one on its outgoing intervals. A processed position cannot return to zero. The maximum has no outgoing intervals under this representation: once it is processed first, it supplies the unique maximum condition automatically.
Each position is selected once and enabled once, and each selection makes a constant number of range operations. Time is and memory is .
Considering only whether a larger witness exists is insufficient: mandatory predecessors must also be respected. Choosing an enabled position solely by its reported distance can violate these constraints.