Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side.
Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4
A simple yet effective approach is to consider each row of the matrix as an array and perform an array rotation. Which can be done by copying the elements from K to end of array to starting of array using temporary array. And then the remaining elements from start to K-1 to end of the arryay.
Lets take an example:
23 34 12 56 67 45 89 91 78
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- How to Left or Right rotate an Array in Java
- Print a matrix in alternate manner (left to right then right to left)
- Rotate Matrix Elements
- Inplace rotate square matrix by 90 degrees | Set 1
- Rotate each ring of matrix anticlockwise by K elements
- Rotate a matrix by 90 degree without using any extra space | Set 2
- Rotate a Matrix by 180 degree
- Rotate a matrix by 90 degree in clockwise direction without using any extra space
- Rotate matrix by 45 degrees
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Print array after it is right rotated K times
- Rotate bits of a number
- Program to cyclically rotate an array by one
- Rotate Linked List block wise
- String slicing in Python to rotate a string
- Rotate Doubly linked list by N nodes
- Python | Ways to rotate a list
- Rotate a Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.