• + 3 comments

    This is the code I used.

    for(i = 0 ; i < mi ; i++) {
        l = 2*((r-2*i) + (c-2*i)) - 4;
        rota = rot;
    
        while( rota >= l )
            rota -= l;
    
        for(j = rota, k = 0 ; k < l ; j++, k++) {
            rs1[i][k] = rs[i][j];
            if( j == (l-1) )
                j = -1;
        }
    }
    

    If the matrix is to be rotated by l times, where l is the length of the layer in question, then essentially no rotation is necessary since every element is in it's initial position at the end of all the rotations.

    So rot is the variable that stores the rotations by input. "rota" is just a temporary variable and it is repeatetively subtracted by l. Then the new array is filled from the new starting position which is "rota" as after n rotations, the nth element in the linear array should be the first.

    The only thing is that I fill another linear array first and then make the matrix from it. Do you think I should just skip this and directly make the array matrix?

    EDIT : Okay nevermind. I tried skipping using the second linear array. Still timed out.