Rotate Matrix Right K Times

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

Explanation

Right rotation on a matrix involves shifting each column to the right (where the last column shifts to the first column). This is done 'K' times as specified.

Input:

The first line of input contains an integer 'T' representing the number of test cases.
Each test case contains:
First line: Three integers 'N', 'M', and 'K' denoting the rows, columns, and the number of rotations respectively.
Next 'N' lines: Each contains 'M' space-separated integers representing the matrix rows.

Output:

For each test case, return the matrix elements row-wise after performing the right rotations as a single line.

Example:

Input:
1
3 3 1
1 2 3
4 5 6
7 8 9
Output:
3 1 2 6 4 5 9 7 8

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 200
  • 1 <= M <= 200
  • 0 <= K <= 10^9
  • 1 <= MAT[i][j] <= 10^5

Note:

You are not required to print the output as it has been handled. Just implement the function to return the result.

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 ro...read more

Help your peers!
Select
Add answer anonymously...

Oyo Rooms Software Developer interview questions & answers

A Software Developer was asked Q. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q2. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q3. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more
Oyo Rooms 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