Left Rotate Matrix K times | Practice | GeeksforGeeks

Given three integers N, M, and K and a matrix Mat of dimensions NxM. Left rotate the matrix K times.
Example 1:
Input:
N=3,M=3,K=1
Mat=[[1,2,3],[4,5,6],[7,8,9]]
Output:
2 3 1
5 6 4
8 9 7
Explanation:
Left rotating the matrix once gives this result.
Example 2:
Input:
N=3,M=3,K=2
Mat=[[1,2,3],[4,5,6],[7,8,9]]
Output:
3 1 2
6 4 5
9 7 8
Explanation:
Left rotating the matrix twice gives this result

Your Task:
You don't need to read input or print anything. Your task is to complete the function rotateMatrix() which takes the three integers N, M, K, and the matrix Mat and returns the matrix Mat left rotated K times.

Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(N*M)

Constraints:
1<=N,M,Mat[i][j]<=1000
1<=K<=10000
0
HEMSINGH MAKWANA9 months ago
HEMSINGH MAKWANA
https://uploads.disquscdn.c...
-1
Taral Jain9 months ago
Taral Jain
int[][] rotateMatrix(int N, int M, int K, int mat[][]) {        K = K % M;         for(int i = 0; i < N; i++){            reverse(mat, i, 0, K - 1);            reverse(mat, i, K, M - 1);            reverse(mat, i, 0, M - 1);        }         return mat;    }     void reverse(int[][] mat, int row, int low, int high){        while(low < high){            int temp = mat[row][low];            mat[row][low] = mat[row][high];            mat[row][high] = temp;            low++; high--;        }    }}
0
kundan Prakash Jha1 year ago
kundan Prakash Jha
vector<vector<int>> rotateMatrix(int n, int m, int k,                                 vector<vector<int>> Mat) {
        vector<vector<int>>vec(n, vector<int>(m));        for(int i=0; i<n; i++)="" {="" for(int="" j="0;" j<m;="" j++)="" {="" int="" t="(k+j)%m;" vec[i][j]="Mat[i][t];" }="" }="" return="" vec;="" }="">
0
kundan Prakash Jha
This comment was deleted.
0
kundan Prakash Jha
This comment was deleted.
0
dilip suthar1 year ago
dilip suthar
class Solution {  public:    vector<vector<int>> rotateMatrix(int n, int m, int k, vector<vector<int>> mat)    {        if(mat.size()==0&&mat[0].size()==0)        {            return {};        }        for(int i=0;i<n;i++) {="" m="mat[i].size();" vector<int="">v(m),temp(m);            k=k%m;            for(int j=0;j<m;j++) {="" v[j]="mat[i][j];" }="" for(int="" j="0;j&lt;m;j++)" {="" temp[((j+(m-k))+m)%m]="v[j];" }="" for(int="" j="0;j&lt;m;j++)" {="" mat[i][j]="temp[j];" }="" }="" return="" mat;="" }="" };<="" spoiler="">
0
dilip suthar1 year ago
dilip suthar
class Solution {  public:    vector<vector<int>> rotateMatrix(int n, int m, int k, vector<vector<int>> mat)    {        if(mat.size()==0&&mat[0].size()==0)        {            return {};        }        for(int i=0;i<n;i++) {="" m="mat[i].size();" vector<int="">v(m),temp(m);            k=k%m;            for(int j=0;j<m;j++) {="" v[j]="mat[i][j];" }="" for(int="" j="0;j&lt;m;j++)" {="" temp[((j+(m-k))+m)%m]="v[j];" }="" for(int="" j="0;j&lt;m;j++)" {="" mat[i][j]="temp[j];" }="" }="" return="" mat;="" }="" };<="" u="">
0
dilip suthar1 year ago
dilip suthar
class Solution {  public:    vector<vector<int>> rotateMatrix(int n, int m, int k, vector<vector<int>> mat)    {        if(mat.size()==0&&mat[0].size()==0)        {            return {};        }        for(int i=0;i<n;i++) {="" m="mat[i].size();" vector<int="">v(m),temp(m);            k=k%m;            for(int j=0;j<m;j++) {="" v[j]="mat[i][j];" }="" for(int="" j="0;j&lt;m;j++)" {="" temp[((j+(m-k))+m)%m]="v[j];" }="" for(int="" j="0;j&lt;m;j++)" {="" mat[i][j]="temp[j];" }="" }="" return="" mat;="" }="" };<="" b="">
0
dilip suthar1 year ago
dilip suthar
class Solution {  public:    vector<vector<int>> rotateMatrix(int n, int m, int k, vector<vector<int>> mat)    {        if(mat.size()==0&&mat[0].size()==0)        {            return {};        }        for(int i=0;i<n;i++) {="" m="mat[i].size();" vector<int="">v(m),temp(m);            k=k%m;            for(int j=0;j<m;j++) {="" v[j]="mat[i][j];" }="" for(int="" j="0;j&lt;m;j++)" {="" temp[((j+(m-k))+m)%m]="v[j];" }="" for(int="" j="0;j&lt;m;j++)" {="" mat[i][j]="temp[j];" }="" }="" return="" mat;="" }="" };="" <="" code="">
0
MD. TASNIN TANVIR2 years ago
MD. TASNIN TANVIR
Easy c++ solutionhttps://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.