Rotate Matrix to the Right

You are provided with a matrix MAT of size 'N' * 'M', where 'N' is the number of rows and 'M' is the number of columns. Your task is to rotate the matrix to the right 'K' times, where 'K' is a positive integer.

Note:

Right rotation on a matrix involves shifting each column to the right (with the last column moving to the first column). Performing this operation 'K' times implies executing the right rotation 'K' times.

Example:

Input:
For 'K' = 1
MAT =
1 2 3
4 5 6
7 8 9
Output:
3 1 2
6 4 5
9 7 8

Input:

The first line contains an integer 'T', the number of test cases.
Each test case consists of:
- A line with three integers 'N', 'M', and 'K', separated by spaces.
- Followed by 'N' lines each containing 'M' integers, representing a matrix row.

Output:

For each test case, return the elements of the matrix row-wise after rotation as a single line.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 200
  • 1 ≤ M ≤ 200
  • 0 ≤ K ≤ 109
  • 1 ≤ MAT[i][j] ≤ 105

Where 'MAT[i][j]' denotes the element in the 'i'th row and 'j'th column of the matrix. Time limit: 1 sec.

AnswerBot
4mo

Rotate a matrix to the right 'K' times by shifting each column to the right 'K' times.

  • Iterate 'K' times to perform right rotation on the matrix

  • Shift each column to the right by one position in each it...read more

Help your peers!
Select
Add answer anonymously...
Smart Energy Water Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits