In this question: Looping in a spiral outside-in, there is a way to "extract the values spirally in a matrix". But what if the requirement is, given an empty n × m matrix, populate it so that it looks something like:
[
[ 1, 2, 3, 4,],
[12,13,14, 5,],
[11,16,15, 6,],
[10, 9, 8, 7,]
]
(so the above is matrix is, given a 4 × 4 matrix, with all 0
s in it, and now populate this matrix so that it looks like the above matrix. Then is there a way like the recursive answer as in Looping in a spiral outside-in ? (Although my concern also is: that solution is elegant, but it is not O(n*m)
)