Hoarding programming problems for you!

Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Rotate Matrix / Image challenge

Sharing is caring!

Problem statement
You are given an n \times n 2D matrix / array representing an image. Rotate matrix by 90 degrees (clockwise) in-place.

We will work through the matrix layers. The layers of a matrix are illustrated below in different colours:

There are a total of \lfloor\dfrac{n}{2}\rfloor layers. We then iterate of the length of each layer and shift our matrix cells, side for side:

We need to temporary store the element of the top side of a layer before we replace it because we are going to need it at the end of the circle top <- left <- bottom <- right <- top. And thats it.

The full code is listed below.

Full code