_ap_ufes{"success":true,"siteUrl":"techblix.com","urls":{"Home":"http://techblix.com","Category":"http://techblix.com/category/3g-4g-plans/","Archive":"http://techblix.com/2017/02/","Post":"http://techblix.com/use-reliance-jio-4g-sim-3g-devices-tricks-proven-work-jio-4g-sim/","Page":"http://techblix.com/guestsponsored-posts/","Attachment":"http://techblix.com/use-reliance-jio-4g-sim-3g-devices-tricks-proven-work-jio-4g-sim/j43g-10/","Nav_menu_item":"http://techblix.com/indianist-2/","Custom_css":"http://techblix.com/sahifa/","Wpcf7_contact_form":"http://techblix.com/?post_type=wpcf7_contact_form&p=5633","Wysijap":"http://techblix.com/?wysijap=subscriptions","Feedback":"http://techblix.com/?post_type=feedback&p=7719"}}_ap_ufee
Breaking News

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
** check other solved codevita questions 

Check Also

reliance jio logo

How to use Reliance Jio 4G SIM on 3G devices – Tricks Proved to Work

Use Jio 4G SIM on 3G devices – 100% tested The main issue with Reliance Jio …

One comment

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

Leave a Reply

error: Right Click is Disabled !!