Given an integer k and a matrix mat[][] of size m x n, left rotate every row by k in a circular fashion.
Examples:
Input: k = 1, mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [[2, 3, 1], [5, 6, 4], [8, 9, 7]]
Explanation: Each row is rotated left by one position.Input: k = 2, mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [[3, 1, 2], [6, 4, 5], [9, 7, 8]]
Explanation: Each row is rotated left by two positions.
Table of Content
[Naive Approach] Rotate Each Row One Step at a Time - O(m * n * k) Time and O(1) Space
The idea is to rotate every row left by one position, k times. In each rotation, shift the elements left and place the first element at the end.
Working of Approach:
- Find the number of rows m and columns n.
- Reduce k using k %= n.
- Traverse every row of the matrix.
- Rotate each row left by one position, k times.
- Return the updated matrix.
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> rotateMatrix(int k, vector<vector<int>> mat)
{
// Get the matrix dimensions.
int m = mat.size();
int n = mat[0].size();
// Reduce k to avoid unnecessary rotations.
k %= n;
// Traverse every row.
for (int i = 0; i < m; i++)
{
// Rotate the current row k times.
for (int rotation = 0; rotation < k; rotation++)
{
// Store the first element of the row.
int first = mat[i][0];
// Shift all elements one position to the left.
for (int j = 0; j < n - 1; j++)
{
mat[i][j] = mat[i][j + 1];
}
// Place the first element at the end.
mat[i][n - 1] = first;
}
}
// Return the rotated matrix.
return mat;
}
int main()
{
int k = 2;
vector<vector<int>> mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<int>> ans = rotateMatrix(k, mat);
cout << "[";
for (int i = 0; i < ans.size(); i++)
{
cout << "[";
for (int j = 0; j < ans[i].size(); j++)
{
cout << ans[i][j];
if (j + 1 < ans[i].size())
{
cout << ", ";
}
}
cout << "]";
if (i + 1 < ans.size())
{
cout << ", ";
}
}
cout << "]" << endl;
return 0;
}
class GFG {
public int[][] rotateMatrix(int k, int[][] mat)
{
int m = mat.length;
int n = mat[0].length;
k %= n;
for (int i = 0; i < m; i++) {
for (int rotation = 0; rotation < k;
rotation++) {
int first = mat[i][0];
for (int j = 0; j < n - 1; j++) {
mat[i][j] = mat[i][j + 1];
}
mat[i][n - 1] = first;
}
}
return mat;
}
public static void main(String[] args)
{
int k = 2;
int[][] mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
GFG obj = new GFG();
int[][] ans = obj.rotateMatrix(k, mat);
System.out.println(
java.util.Arrays.deepToString(ans));
}
}
def rotateMatrix(k, mat):
# Get the matrix dimensions.
m = len(mat)
n = len(mat[0])
# Reduce k to avoid unnecessary rotations.
k %= n
# Traverse every row.
for i in range(m):
# Rotate the current row k times.
for rotation in range(k):
# Store the first element of the row.
first = mat[i][0]
# Shift all elements one position to the left.
for j in range(n - 1):
mat[i][j] = mat[i][j + 1]
# Place the first element at the end.
mat[i][n - 1] = first
# Return the rotated matrix.
return mat
if __name__ == "__main__":
k = 2
mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
ans = rotateMatrix(k, mat)
print(ans)
using System;
class GFG {
public int[][] rotateMatrix(int k, int[][] mat)
{
int m = mat.Length;
int n = mat[0].Length;
k %= n;
for (int i = 0; i < m; i++) {
for (int rotation = 0; rotation < k;
rotation++) {
int first = mat[i][0];
for (int j = 0; j < n - 1; j++) {
mat[i][j] = mat[i][j + 1];
}
mat[i][n - 1] = first;
}
}
return mat;
}
public static void Main(string[] args)
{
int k = 2;
int[][] mat = { new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9 } };
GFG obj = new GFG();
int[][] ans = obj.rotateMatrix(k, mat);
Console.Write("[");
for (int i = 0; i < ans.Length; i++) {
Console.Write("[");
for (int j = 0; j < ans[i].Length; j++) {
Console.Write(ans[i][j]);
if (j < ans[i].Length - 1)
Console.Write(", ");
}
Console.Write("]");
if (i < ans.Length - 1)
Console.Write(", ");
}
Console.WriteLine("]");
}
}
function rotateMatrix(k, mat)
{
// Get the matrix dimensions.
const m = mat.length;
const n = mat[0].length;
// Reduce k to avoid unnecessary rotations.
k %= n;
// Traverse every row.
for (let i = 0; i < m; i++) {
// Rotate the current row k times.
for (let rotation = 0; rotation < k; rotation++) {
// Store the first element of the row.
const first = mat[i][0];
// Shift all elements one position to the left.
for (let j = 0; j < n - 1; j++) {
mat[i][j] = mat[i][j + 1];
}
// Place the first element at the end.
mat[i][n - 1] = first;
}
}
// Return the rotated matrix.
return mat;
}
// Driver Code
const k = 2;
const mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
const ans = rotateMatrix(k, mat);
console.log(JSON.stringify(ans).replace("],", "],\n"));
Output
[[3, 1, 2], [6, 4, 5], [9, 7, 8]]
[Expected Approach] Using Direct Circular Rotation - O(m * n) Time and O(m * n) Space
The idea is to directly calculate the new position of every element after rotating the row left by k positions using circular indexing. Store the elements at their calculated positions in a new matrix.

Working of Approach:
- Find the number of rows m and columns n.
- Reduce k using k %= n.
- Create a result matrix of the same size.
- For every element, calculate its new column using circular indexing.
- Place the element at its new position and return the result.
Let us understand with an example:
Input: k = 2, mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- Since each row has n = 3 elements, k %= n gives k = 2.
- For the first row, newCol = (j - k + n) % n, so [1, 2, 3] becomes [3, 1, 2].
- Similarly, [4, 5, 6] becomes [6, 4, 5] and [7, 8, 9] becomes [9, 7, 8].
- Output: [[3, 1, 2], [6, 4, 5], [9, 7, 8]].
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> rotateMatrix(int k, vector<vector<int>> mat)
{
// Get the matrix dimensions.
int m = mat.size();
int n = mat[0].size();
// Reduce k to avoid unnecessary rotations.
k %= n;
// Create the rotated matrix.
vector<vector<int>> res(m, vector<int>(n));
// Rotate every row to the left by k positions.
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
int newCol = (j - k + n) % n;
res[i][newCol] = mat[i][j];
}
}
// Return the rotated matrix.
return res;
}
int main()
{
int k = 2;
vector<vector<int>> mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<int>> ans = rotateMatrix(k, mat);
cout << "[";
for (int i = 0; i < ans.size(); i++)
{
cout << "[";
for (int j = 0; j < ans[i].size(); j++)
{
cout << ans[i][j];
if (j + 1 < ans[i].size())
{
cout << ", ";
}
}
cout << "]";
if (i + 1 < ans.size())
{
cout << ", ";
}
}
cout << "]" << endl;
return 0;
}
import java.util.Arrays;
class GFG {
public int[][] rotateMatrix(int k, int[][] mat)
{
// Get the matrix dimensions.
int m = mat.length;
int n = mat[0].length;
// Reduce k to avoid unnecessary rotations.
k %= n;
// Create the rotated matrix.
int[][] res = new int[m][n];
// Rotate every row to the left by k positions.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int newCol = (j - k + n) % n;
res[i][newCol] = mat[i][j];
}
}
// Return the rotated matrix.
return res;
}
public static void main(String[] args)
{
int k = 2;
int[][] mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
GFG obj = new GFG();
int[][] ans = obj.rotateMatrix(k, mat);
System.out.println(Arrays.deepToString(ans));
}
}
def rotateMatrix(k, mat):
# Get the matrix dimensions.
m = len(mat)
n = len(mat[0])
# Reduce k to avoid unnecessary rotations.
k %= n
# Create the rotated matrix.
res = [[0] * n for _ in range(m)]
# Rotate every row to the left by k positions.
for i in range(m):
for j in range(n):
newCol = (j - k + n) % n
res[i][newCol] = mat[i][j]
# Return the rotated matrix.
return res
if __name__ == "__main__":
k = 2
mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
ans = rotateMatrix(k, mat)
print(ans)
using System;
class GFG {
public int[][] rotateMatrix(int k, int[][] mat)
{
// Get the matrix dimensions.
int m = mat.Length;
int n = mat[0].Length;
// Reduce k to avoid unnecessary rotations.
k %= n;
// Create the rotated matrix.
int[][] res = new int[m][];
for (int i = 0; i < m; i++) {
res[i] = new int[n];
}
// Rotate every row to the left by k positions.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int newCol = (j - k + n) % n;
res[i][newCol] = mat[i][j];
}
}
// Return the rotated matrix.
return res;
}
public static void Main(string[] args)
{
int k = 2;
int[][] mat = { new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9 } };
GFG obj = new GFG();
int[][] ans = obj.rotateMatrix(k, mat);
Console.Write("[");
for (int i = 0; i < ans.Length; i++) {
Console.Write("[");
for (int j = 0; j < ans[i].Length; j++) {
Console.Write(ans[i][j]);
if (j + 1 < ans[i].Length) {
Console.Write(", ");
}
}
Console.Write("]");
if (i + 1 < ans.Length) {
Console.Write(", ");
}
}
Console.WriteLine("]");
}
}
function rotateMatrix(k, mat)
{
// Get the matrix dimensions.
const m = mat.length;
const n = mat[0].length;
// Reduce k to avoid unnecessary rotations.
k %= n;
// Create the rotated matrix.
const res
= Array.from({length : m}, () => Array(n).fill(0));
// Rotate every row to the left by k positions.
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const newCol = (j - k + n) % n;
res[i][newCol] = mat[i][j];
}
}
// Return the rotated matrix.
return res;
}
// Driver Code
const k = 2;
const mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
const ans = rotateMatrix(k, mat);
console.log("[");
for (let i = 0; i < ans.length; i++) {
console.log("[");
for (let j = 0; j < ans[i].length; j++) {
console.log(ans[i][j]);
if (j + 1 < ans[i].length) {
console.log(", ");
}
}
console.log("]");
if (i + 1 < ans.length) {
console.log(", ");
}
}
console.log("]");
Output
[[3, 1, 2], [6, 4, 5], [9, 7, 8]]