i totally understood this video but was unable to understand how to implement transpose of matrix and row transforms in code…
Doubt regarding interview problem image rotation
Hey, taradeep ,can transpose the given matrix with below pseudo code.
A=given matrix, B=matrix you get after transpose
for(int i=0;i<n;i++)
{ for(int j=0;j<N;j++)
{
B[i][j]=A[j][i];
}
}
for revserving the row
for(i =0;i<N;i++)
{
for(int j=0;j<N/2;j++)
{
B[i][j]=B[i][N-1-j];
}
B[i][j]=B[i][N-1-j];
}
can u explain ur code for reversing row?
If you carefully analyze the situation, then you will realize that if you have input as :
1 2 3
4 5 6
7 8 9
Thus after transposing, you get as
1 4 7
2 5 8
3 6 9
The final ans that you need is
7 4 1
8 5 2
9 6 3
This answer can be obtained from transpose simply by reversing all rows, but while you are reversing, the important thing to consider is that the middle column remains same in transposed matrix as well as final ans matrix, that is why you will use the inner loop running from 0 to N/2, to ensure that the middle column remains same, only the 1st and 3rd columns are interchanged
hey @taran9873, a single row is linear array, jth element from last is given by N-1-jth element from last. So if you swap the jth elmennt from begnning and jth element from last than and running loop till last, you will get reverse of that linear array.
corrected code for reversing
for(i =0;i<N;i++)
{
for(int j=0;j<N/2;j++)
{
swap(B[i][j]=B[i][N-1-j]);
}
consider this pic for explanation.
hey @taran9873 , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved