Sort a Matrix into Increasing Zigzag Order Using Adjacent Swaps

Given an n × n integer matrix grid containing every integer from 1 to n² exactly once, transform it into increasing row-wise zigzag order using adjacent swaps only.

The target layout is: ...

Example

Unlock to view complete problem details

and practice with sample input/output

Was this article helpful?

View Test Cases & Run Code requires membership

Standard Input
Execution Result: