AfterAcademy Tech
•
11 Feb 2020

Difficulty: Medium
Asked in: Google
You are given an N x N 2D matrix representing an image. Write a program to rotate the image by 90 degrees (clockwise).
Problem Note
Example 1
Input:
[
[1,2,3],
[4,5,6],
[7,8,9]
]
Output: Rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2
Input:
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
]
Output: Rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
You may try to solve the problem here.
We will be discussing two approaches for the problem
The idea is to first, transpose the matrix and then flip it symmetrically. For instance,
1 2 3
4 5 6
7 8 9
after transpose, it will be swap(matrix[i][j], matrix[j][i]) i.e. make the rows as columns and columns as rows.
1 4 7
2 5 8
3 6 9
Then flip the matrix along the vertical axis. swap(matrix[i][j], matrix[i][N-1-j]
7 4 1
8 5 2
9 6 3
Visualization

Solution steps
i and j indices within the range of its length.matrix[i][j] with matrix[j][i].2. Reverse each row of the matrix
matrix[i][j] with matrix[i][N-1-j]Pseudo Code
// Driver function, N represent the dimensions of matrix
void rotateMatrix(int[][] matrix, int N) {
// take transpose of matrix
for(int i = 0 to N){
for(int j = i to N){
int temp = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = temp
}
}
// perform rotation of each row
for(int i = 0 to N){
for(int j = 0 to N/2){
int temp = matrix[i][j]
matrix[i][j] = matrix[i][N-1-j]
matrix[i][N-1-j] = temp
}
}
}
Critical Ideas to Think
N/2 while rotating each row?N x N?Complexity Analysis
Time Complexity — O(N²) (Why?)
Space Complexity — O(1) Due to in-place rotation of matrix.
If you will notice the input and resultant matrices, you will find that,
The first row of the source matrix is the last column of the destination matrix. Similarly, the second row of the source matrix is the second last column of destination and so … on the last row of the source is the last column of the destination matrix.

So, if we consider the outer most elements of the matrix as a layer then a N x N matrix will have ceil(N/2) square cycles. For example, a 6 x 6 matrix will have 3 cycles. The first cycle is formed by its 1st row, last column, last row and 1st column. The second cycle is formed by 2nd row, second-last column, second-last row and 2nd column and so on. (You can take help from the above figure where N = 5)
The idea is for each square cycle, we swap the elements involved with the corresponding cell in the matrix in clockwise direction i.e. from top to right, left to top, bottom to the left and from right to the bottom, one at a time.
Visualization

Solution Steps
N/2) cycles for any N.Pseudo Code
void rotateMatrix(int[][] matrix, int N) {
int layer = 0
while(layer < N/2){
int low = layer
int high = N - 1 - layer
for (int i = low to high){
// Swapping elements after each iteration in clockwise
int temp = matrix[low][i]
matrix[low][i] = matrix[N-1-i][low]
matrix[N-1-i][low] = matrix[N-1-low][N-1-i]
matrix[N-1-low][N-1-i] = matrix[i][N-1-low]
matrix[i][N-1-low] = temp
}
layer = layer + 1
}
}
Critical Ideas to Think
N/2 cycles for a particular N?Complexity Analysis
Time Complexity — O(N²)
Space Complexity — O(1), Rotation is performed in-place.

Please comment down below if you find an improvement in the above explanation.
Happy Coding!
AfterAcademy Tech
You have a sorted and rotated array where elements are sorted and rotated circularly. Write a program to find the minimum element in the array.

AfterAcademy Tech
Given a matrix or a 2-d array, you have to traverse the matrix in a spiral order. This is an interview question asked by companies like Microsoft and Amazon.

AfterAcademy Tech
You are given a matrix arr of m x n size. Write a program to searches for a value k in arr. This arr has the following properties: - Integers in each row are sorted from left to right. - The first value of each row is greater than the last value of previous row. This is a basic optimization problem that will clear the concept of searching.

AfterAcademy Tech
Given a matrix, A of size M x N of 0's and 1's. If an element is 0, set its entire row and column to 0
