Click here to Skip to main content
14,977,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Rotate Matrix Rings
Given a matrix of order M*N and a value K, write a program to rotate each ring of the matrix clockwise by K elements. If in any ring has less than or equal to K elements, then don’t rotate that ring.

The first line of input will be two space-separated integers, denoting the M and N.
The next M lines will contain N space-separated integers.
The next line will contain an integer, denoting K.

The output should be M*N matrix by rotating the matrix by K elements.

For example, if the given M and N are 4 and 4 respectively. If the matrix elements are
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
If the given K is 3. Rotate each ring of the matrix by 3 elements.
In the above matrix, the elements (1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5) is a ring, similarly, the elements (6, 7, 11, 10) will make a ring.
Therefore, by rotating each ring in clockwise direction by 3 elements will give (13, 9, 5, 1, 2, 3, 4, 8, 12, 16, 15, 14) and (10, 6, 7, 11). So the output should be
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4

What I have tried:

M, N = input().split()
M, N = int(M), int(N)
matrix = []
for _ in range(M):
	row = [int(x) for x in input().split(' ')]

K = int(input())

values = matrix[0][:-1] + [x[-1] for x in matrix][:-1] + matrix[-1][::-1][:-1] + [x[0] for x in matrix][::-1][:-1]

values = values[-K:] + values[:-K]

output = matrix

idxs = [(0, j) for j in range(N)][:-1] + [(i, N - 1) for i in range(M)][:-1] + [(M - 1, j) for j in range(N)][::-1][:-1] + [(i, 0) for i in range(M)][::-1][:-1]

idx = 0
for i, j in idxs:
	output[i][j] = values[idx]
	idx += 1

Updated 28-Apr-21 14:27pm
Patrice T 4-Apr-21 23:21pm
And you have a question or a problem ?
BOBBY edits 4-Apr-21 23:23pm
Patrice T 5-Apr-21 0:14am
And you plan to tell which one ?
BOBBY edits 5-Apr-21 2:17am
I am getting the wrong rotations
I changed code so many times but still, It doesn't satisfy the conditions
Patrice T 5-Apr-21 2:19am
Use Improve question to update your question.
So that everyone can pay attention to this information.
OriginalGriff 5-Apr-21 2:07am
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So if you just say "I got a problem" then we have no idea what problem you have found, where, or what data you used to find it.

Use the "Improve question" widget to edit your question and provide better information.
BOBBY edits 5-Apr-21 2:18am
I have to get k rotations but
I am getting the wrong rotations
I changed code so many times but still, It doesn't satisfy the conditions
EddieDover 29-Apr-21 17:09pm
Check the solution I've provided you. I used it in a puzzle for a MUD where the player had to rotate a ring of symbols until the proper set lined up with a crystal in the center.

I have to get k rotations but
I am getting the wrong rotations
I changed code so many times but still, It doesn't satisfy the conditions

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   return value * value;

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
Here's a modified snippet from a MUD I was working on a few years ago:
#include <iostream>

int main()

    int matrix[7][7] =
    int rows,cols;
    rows = cols = *(&matrix + 1) - matrix;
    // int cols = *(&matrix[0] + 1) - matrix[0];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            std::cout << matrix[i][j];
        std::cout << std::endl;
    std::cout << std::endl;


    int turncircle = 2; //This is the variable of the ring you'll rotate. From the inside (0 is center);

    int circles = (rows - 1) / 2;
    int upperlimit = (turncircle + circles);
    int lowerlimit = (circles - turncircle);
    int ri;
    int rj;

    rj = lowerlimit;
    ri = upperlimit;

    int corners[] = {matrix[upperlimit][lowerlimit],
    for (ri = upperlimit; ri > (lowerlimit - 1); ri--)
        if (ri != upperlimit)
            matrix[rj][ri + 1] = matrix[rj][ri];
    rj = upperlimit;
    for (ri = lowerlimit; ri < upperlimit + 1; ri++)
        if (ri != lowerlimit)
            matrix[rj][ri-1] = matrix[rj][ri];
    ri = upperlimit;
    for (rj = upperlimit; rj > (lowerlimit - 1); rj--)
        if (rj != upperlimit)
            matrix[rj + 1][ri] = matrix[rj][ri];
    ri = lowerlimit;
    for (rj = lowerlimit; rj < (upperlimit + 1); rj++)
        if (rj != lowerlimit)
            matrix[rj - 1][ri] = matrix[rj][ri];
    matrix[lowerlimit + 1][upperlimit] = corners[0];
    matrix[upperlimit - 1][lowerlimit] = corners[1];

    // END OF ALGO

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            std::cout << matrix[i][j];
        std::cout << std::endl;
    std::cout << std::endl;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900