TCS CodeVita previous years questions Solved – Matrix Rotation
TCS CodeVita previous years questions – 2014
Problem : Matrix Rotations
Constraints:
Sample Input and Output
TCS CodeVita previous years questions – Matrix Rotation solution
Sample Solution C proram code :
Problem : Matrix Rotations
You are given a square matrix of dimension N. Let this matrix be called A. Your task is to rotate A in clockwise direction byS 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
- Querying
Query the element at row K and column L, presented as input in form of Q K L
- Updation
Update the element at row X and column Y with value Z, presented as input in form of U X Y Z
Print the output of individual operations as depicted in Output Specification
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.
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.
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
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
1 2
3 4
After 90 degree rotation, the matrix will become
3 1
4 2
Now the element at A11 is 3 and A12 is 1.
Again the angle of rotation is 90 degree, now after the rotation the matrix will become
4 3
2 1
Now the element at A11 is 4.
As the next operation is Update, update initial matrix i.e.
6 2
3 4
After updating, apply all the previous rotations (i.e. 180 = two 90 degree rotations).
The matrix will now become
4 3
2 6
Now A22 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
Comments
best laptop for students
best gaming laptop under 50k
best gaming laptop under 50000
best laptop under 50k
best laptop under 40K
best laptop under 40,000
best smartphones under 10000
best smartphones under 10k
best gaming smartphones under 10000
best camera smartphones under 10000
best camera smartphones under 15000
best gaming smartphones under 15000
best battery smartphones under 15000
best camera smartphones under 15k
best gaming smartphones under 15k
best battery smartphones under 15k
best smartphones under 15k
Top 10 Certified Python Training Institute in Delhi, NCR
Core to Advanced AutoCAD training institute in Delhi, NCR
Best Certified Software Testing Training Institute in Delhi, NCR
Choose the Right Institute for MS Office Training Course
Core to Advanced Cloud Computing Training Course in Delhi, India
ISO Certified AWS Training Institute in Delhi, NCR