TCS CodeVita previous years questions Solved – Matrix Rotation


TCS CodeVita previous years questions – 2014
TCS CodeVita previous years questions with solutions


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.
  1. Rotation
    Rotate the matrix A by angle S, presented as input in form of A S
  2. Querying
    Query the element at row K and column L, presented as input in form of Q K L
  3. 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.
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
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

Thank you so much for sharing these amazing tips. I must say you are an incredible writer, I love the way that you describe the things. Please keep sharing.

Top 10 Certified Python Training Institute in Delhi, NCR
Core to Advanced AutoCAD training institute in Delhi, NCR

Great job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us.

Best Certified Software Testing Training Institute in Delhi, NCR
Choose the Right Institute for MS Office Training Course

You have a real ability to write a content that is helpful for us. Thank you for your efforts in sharing such blogs to us. Keep sharing this in future.

Core to Advanced Cloud Computing Training Course in Delhi, India
ISO Certified AWS Training Institute in Delhi, NCR

Popular posts from this blog

A Ten Year Journey: How SEO PowerSuite Has Kept Its SEO Tools Current

4 Ways to Crack a Facebook Password and How to Protect Yourself from Them

Create Separate Contact Page in Blogger