 # Unlocker | TCS Codevita 2020

This is one of the Questions of Tcs codevita 2020, It was giving Memory limit exceed while submitting and in Online compiler it gives TLE.

Image 1

Image 2

Image 3

My approach->>>

Simple Find the Spiral order traversal of the matrix and then
a) If we have matrix of m x n then it have total boundary elements = 2(m) + 2(n-2)
b) Simply use this formula and left shift or right shift the elements based on the {Condition that , it is odd or even layer}
c) And finally Placed all the elements back to the matrix again and print the final matrix

My Code

Please Let me know Why I am getting Memory limit/TLE Error.

I thought of flattening all the layers and store as list of list, but while doing that I made some calculation mistakes and was failing for some test cases, couldn’t fix those, and also take modulus of the number of rotations to the length of layer as it could be as high as 10^9.

I M already taking modulus brother, and N=300 , So i don’t understand why giving tle in this.

Here’s my AC solution.
Feel free to ask any queries
``````//package TCS_Codevita9_QualificationRound;

import java.util.*;

public class D {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int c = sc.nextInt();

int[][] locker = new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
locker[i][j] = sc.nextInt();
}
}

int l = Math.min(r,c)/2;
int[] arr = new int[l];
for(int i=0;i<l;i++)
{
arr[i] = sc.nextInt();
}

int[][] output = new int[r][c];
for(int i=0;i<l;i++)
{
int totalnumberofelements = ((c-2*i)*2)+(r-2*(i+1))*2;
arr[i] %= totalnumberofelements;

if((i+1)%2!=1) {
arr[i] = totalnumberofelements-arr[i];
}
int[] linerarr = new int[totalnumberofelements];
int k=0;
for(int i1=i;i1<c-i;i1++)
{
linerarr[k] = locker[i][i1];
k++;
}
for(int i1=i+1;i1<r-i;i1++)
{
linerarr[k] = locker[i1][c-i-1];
k++;
}
for(int i1=c-2-i;i1>=i;i1--)
{
linerarr[k] = locker[r-i-1][i1];
k++;
}
for(int i1=r-i-2;i1>i;i1--)
{
linerarr[k] = locker[i1][i];
k++;
}

int[] shifted = new int[totalnumberofelements];
for(int i1=0;i1<totalnumberofelements;i1++)
{
int ind = (i1 + arr[i])%totalnumberofelements;
shifted[i1] = linerarr[ind];
}

k=0;
for(int i1=i;i1<c-i;i1++)
{
output[i][i1] = shifted[k];
k++;
}
for(int i1=i+1;i1<r-i;i1++)
{
output[i1][c-i-1] = shifted[k];
k++;
}
for(int i1=c-2-i;i1>=i;i1--)
{
output[r-i-1][i1] = shifted[k];
k++;
}
for(int i1=r-i-2;i1>i;i1--)
{
output[i1][i] = shifted[k];
k++;
}
}

for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(output[i][j]+" ");
}
System.out.println();
}
}
}
``````

Here is my code…I think you haven’t performed modulo function on k.
If you have any question feel ask them

Brother I also apply the modulo on K , can you please check my code once, Still I got memory limit exceeded , Please debug and tell why?