Rotate Matrix - Clockwise - C Program - O(n) space complexity


int A[4][6] = {{90,91,92,93,94,95},{64,65,66,67,68,69},{12,13,14,15,16,17},{18,19,20,21,22,23}};
int B[6][4];
int main()
{
int i,j;

/* Print Array A[4][6] */
for(i=0;i<4;i++)
{
for(j=0;j<6;j++)
printf("%d ",A[i][j]);
printf("\n");
}

/* Rotate the array */
for(i=0;i<4;i++)
for(j=0;j<6;j++)
B[j][4-i-1] = A[i][j];


/* Print Array b[6][4] */
printf("\n\n");
for(i=0;i<4;i++)
{
for(j=0;j<6;j++)
printf("%d ",B[i][j]);
printf("\n");
}


return 0;
}


No comments:

Post a Comment