TCS CodeVita previous years questions – 2014

**Problem : Matrix Rotations**

**N**. Let this matrix be called

**A**. Your task is to rotate

**A**in clockwise direction by

**S**degrees, where

**S**is angle of rotation. On the matrix, there will be 3 types of operations viz.

- Rotation
Rotate the matrix A by angle S, presented as input in form of

**A S** **Q**ueryingQuery the element at row K and column L, presented as input in form of

**Q K L****U**pdationUpdate the element at row X and column Y with value Z, presented as input in form of

**U X Y Z**

**Input Format:**

Input will consist of three parts, viz.

1. Size of the matrix (N)

2. The matrix itself (A = N * N)

3. Various operations on the matrix, one operation on each line. (Beginning either with A, Q or U)

-1 will represent end of input.

**Note:**

- Angle of rotation will always be multiples of 90 degrees only.
- All Update operations happen only on the initial matrix. After update all the previous rotations have to be applied on the updated matrix

**Output Format:**

For each Query operation print the element present at K-L location of the matrix in its current state.

**Constraints:**

**1<=N<=1000**

**1<=Aij<=1000**

**0<=S<=160000**

**1<=K, L<=N**

**1<=Q<=100000**

**Sample Input and Output**

SNo. | Input | Output |
---|---|---|

1 | 2 1 2 3 4 A 90 Q 1 1 Q 1 2 A 90 Q 1 1 U 1 1 6 Q 2 2 -1 |
3 1 4 6 |

**Explanation:**

**Initial Matrix**

_{11}is 3 and A

_{12}is 1.

_{11}is 4.

**Update**, update initial matrix i.e.

_{22}is 6.

TCS CodeVita previous years questions – Matrix Rotation solution

**Sample Solution C proram code :**

#include<stdio.h> int i,j,n,arr1[1000][1000],arr2[1000][1000]; void swaparray() { for(i=0;i<n;i++) for(j=0;j<n;j++) arr1[i][j]=arr2[i][j]; } int main() { char opt[4]; scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&arr1[i][j]); scanf("%s",opt); while(strcmp(opt,"-1")) { if(opt[0]=='A') { if(!strcmp(opt,"A90")) { for(i=0;i<n;i++) for(j=0;j<n;j++) arr2[j][n-i-1]=arr1[i][j]; swaparray(); } else if(!strcmp(opt,"A180")) { for(i=0;i<n;i++) for(j=0;j<n;j++) arr2[n-i-1][n-j-1]=arr1[i][j]; swaparray(); } else if(!strcmp(opt,"A270")) { for(i=0;i<n;i++) for(j=0;j<n;j++) arr2[n-j-1][i]=arr1[i][j]; swaparray(); } else if(!strcmp(opt,"A0") || !strcmp(opt,"A360")); //not required } else if(opt[0]=='Q') { if(opt[1]-48<n && opt[2]-48<n) printf("%d\n",arr1[opt[1]-48][opt[2]-48]); } else if(opt[0]='U') { if(opt[1]-48<n && opt[2]-48<n) arr1[opt[1]-48][opt[2]-48]=opt[3]-48; } scanf("%s",opt); } return 0; } //validate the constraints too ** check other solved codevita questions

no need to rotate the matrix itself. just query in angle .