Given a matrix mat[][] of dimensions M * N, the task is to print the matrix obtained after rotating every ith row of the matrix i times in a clockwise direction.
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output:
1 2 3
6 4 5
8 9 7
Explanation:
The 0th row is rotated 0 times. Therefore, the 0th row remains the same as {1, 2, 3}.
The 1st row is rotated 1 times. Therefore, the 1st row modifies to {6, 4, 5}.
The 2nd row is rotated 2 times. Therefore, the 2nd row modifies to {8, 9, 7}.
After completing the above operations, the given matrix modifies to {{1, 2, 3}, {6, 4, 5}, {8, 9, 7}}.Input: mat[][] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 8}, {7, 8, 9, 8}}
Output:
1 2 3 4
7 4 5 6
9 8 7 8
8 9 8 7
Approach: Follow the steps below to solve the problem:
- Traverse the given matrix in row – wise manner and for every ith row, perform the following steps:
- Reverse the current row of the matrix.
- Reverse the first i elements of the current row.
- Reverse the last (N – i) elements of the current row, where N is the current size of the row.
- After completing the above steps, print the matrix mat[][].
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to rotate every i-th // row of the matrix i times void rotateMatrix(vector<vector< int > >& mat) { int i = 0; // Traverse the matrix row-wise for ( auto & it : mat) { // Reverse the current row reverse(it.begin(), it.end()); // Reverse the first i elements reverse(it.begin(), it.begin() + i); // Reverse the last (N - i) elements reverse(it.begin() + i, it.end()); // Increment count i++; } // Print final matrix for ( auto rows : mat) { for ( auto cols : rows) { cout << cols << " " ; } cout << "\n" ; } } // Driver Code int main() { vector<vector< int > > mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; rotateMatrix(mat); return 0; } |
Java
// Java program for the above approach import java.util.*; class Main { // Function to rotate every i-th row of the matrix i // times static void rotateMatrix(List<List<Integer> > mat) { int i = 0 ; // Traverse the matrix row-wise for (List<Integer> it : mat) { // Reverse the current row Collections.reverse(it); // Reverse the first i elements Collections.reverse(it.subList( 0 , i)); // Reverse the last (N - i) elements Collections.reverse(it.subList(i, it.size())); // Increment count i++; } // Print final matrix for (List<Integer> rows : mat) { for ( int cols : rows) { System.out.print(cols + " " ); } System.out.println(); } } // Driver Code public static void main(String[] args) { List<List<Integer> > mat = new ArrayList<>(); mat.add(Arrays.asList( 1 , 2 , 3 )); mat.add(Arrays.asList( 4 , 5 , 6 )); mat.add(Arrays.asList( 7 , 8 , 9 )); rotateMatrix(mat); } } |
1 2 3 6 4 5 8 9 7
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(1), as we are not using any extra space.
Please refer complete article on Modify a matrix by rotating ith row exactly i times in clockwise direction for more details!