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

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

    def rotateMatrix(self,N,M,K,Mat):

        #code here

Solutions:
class Solution:

	def rotateMatrix(self, N, M, K, matrix):
		num_rows = len(matrix)
		num_cols = len(matrix[0])
		rotated_matrix = [[0] * num_cols for _ in range(num_rows)]
		for row in range(num_rows):
			for col in range(num_cols):
				new_col = (col - K) % num_cols
				rotated_matrix[row][new_col] = matrix[row][col]
		return rotated_matrix


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		res = []
		for i in Mat:
			res.append(i[K % M:] + i[:K % M])
		return res


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		mymat = [[Mat[i][j] for j in range(M)] for i in range(N)]
		K %= M
		for i in range(N):
			for j in range(M):
				if j + K < M:
					Mat[i][j] = mymat[i][j + K]
				else:
					Mat[i][j] = mymat[i][j + K - M]
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for i in range(N):
			arr = Mat[i].copy()
			k = 0
			for j in range(M - K, M - K + M):
				j = j % M
				Mat[i][j] = arr[k]
				k += 1
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K %= M
		for i in range(N):
			Mat[i] = self.func(Mat[i], K)
		return Mat

	def func(self, arr, K):
		arr[:] = arr[K:] + arr[:K]
		return arr


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		return [row[K:] + row[:K] for row in Mat]


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M

		def rotate(row):
			v = M - K
			row = row[::-1]
			row[:v] = row[:v][::-1]
			row[v:] = row[v:][::-1]
			return row
		for (i, row) in enumerate(Mat):
			Mat[i] = rotate(row)
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		matrix = []
		for i in Mat:
			rotate_times = K % len(Mat[0])
			first_part = i[0:rotate_times]
			second_part = i[rotate_times:]
			second_part.extend(first_part)
			matrix.append(second_part)
		return matrix


class Solution:

	def reverse(self, arr, li, ri):
		i = li
		j = ri
		while i < j:
			(arr[i], arr[j]) = (arr[j], arr[i])
			i += 1
			j -= 1

	def rotateMatrix(self, n, m, k, mat):
		if k >= m:
			k = k % m
		if k < 0:
			k += m
		for i in range(n):
			self.reverse(mat[i], 0, k - 1)
			self.reverse(mat[i], k, m - 1)
		for i in range(n):
			self.reverse(mat[i], 0, m - 1)
		return mat


class Solution:

	def rotateMatrix(self, n, m, k, mat):

		def rotate(arr, n, k):
			lar = max(arr) + 1
			start = k % n
			j = 0
			for i in range(start, start + n):
				arr[j] += arr[i % n] % lar * lar
				j += 1
			for i in range(n):
				arr[i] = arr[i] // lar
		for i in range(n):
			rotate(mat[i], m, k)
		return mat


import numpy as np

class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % len(Mat[0])
		for row in Mat:
			for _ in range(K):
				row.append(row[0])
				row.pop(0)
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for i in Mat:
			i.extend(i)
			i[:] = i[K:K + M]
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		ans = [[0 for i in range(M)] for j in range(N)]
		for i in range(N):
			for j in range(M):
				t = (K + j) % M
				ans[i][j] = Mat[i][t]
		return ans


class Solution:

	def rotateMatrix(self, N, M, K, Mat):

		def swap(arr, i, j):
			while i <= j:
				(arr[i], arr[j]) = (arr[j], arr[i])
				i += 1
				j -= 1

		def rotate(arr, k):
			n = len(arr)
			i = 0
			j = k - 1
			swap(arr, i, j)
			i = k
			j = n - 1
			swap(arr, i, j)
			i = 0
			j = n - 1
			swap(arr, i, j)
		K = K % M
		for i in range(N):
			rotate(Mat[i], K)
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		l = []
		k = []
		for i in Mat:
			for j in range(K % M, M):
				l.append(i[j])
			for a in range(K % M):
				l.append(i[a])
			k.append(l)
			l = []
		return k


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		r = K % M
		if r == 0 and r == M:
			return Mat
		else:
			for i in range(N):
				Mat[i] = Mat[i][r:] + Mat[i][:r]
			return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for i in range(len(Mat)):
			a = Mat[i]
			x = a[K:]
			x += a[:K]
			Mat[i] = x
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		res = []
		for i in range(N):
			row = []
			K = K % M
			for j in range(K, M):
				row.append(Mat[i][j])
			for l in range(0, K):
				row.append(Mat[i][l])
			res.append(row)
		return res


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		newMat = [[0] * (2 * M) for _ in range(N)]
		for i in range(N):
			for j in range(M):
				newMat[i][j] = Mat[i][j]
				newMat[i][j + M] = Mat[i][j]
		realk = K % M
		x = y = 0
		for i in range(N):
			y = 0
			for j in range(realk, realk + M):
				Mat[x][y] = newMat[i][j]
				y += 1
			x += 1
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		from collections import deque
		ans = []
		for i in Mat:
			temp = deque(i)
			temp.rotate(-K)
			temp = list(temp)
			ans.append(temp)
		return ans


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for itr in range(N):
			for itr2 in range(K):
				Mat[itr].append(Mat[itr].pop(0))
		return Mat
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M, K) = map(int, input().strip().split(' '))
		Mat = []
		for i in range(N):
			Mat.append(list(map(int, input().strip().split(' '))))
		ob = Solution()
		ans = ob.rotateMatrix(N, M, K, Mat)
		for i in range(N):
			for j in range(M):
				print(ans[i][j], end=' ')
			print()


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		b = []
		K = K % M
		for i in Mat:
			c = i[:K]
			d = i[K:]
			d.extend(c)
			b.append(d)
		return b
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M, K) = map(int, input().strip().split(' '))
		Mat = []
		for i in range(N):
			Mat.append(list(map(int, input().strip().split(' '))))
		ob = Solution()
		ans = ob.rotateMatrix(N, M, K, Mat)
		for i in range(N):
			for j in range(M):
				print(ans[i][j], end=' ')
			print()


class Solution:

	def rotateMatrix(self, r, c, k, mat):
		k = k % c
		result = [row[:] for row in mat]
		for i in range(c):
			temp = [mat[j][i] for j in range(r)]
			insertColIndex = (i - k) % c
			for j in range(r):
				val = temp[j]
				result[j][insertColIndex] = val
		return result


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		mod = K % M
		for i in range(N):
			Mat[i] = Mat[i][mod:] + Mat[i][:mod]
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		rem = K % M
		result_matrix = []
		for i in range(0, N):
			new_list = Mat[i][rem:] + Mat[i][:rem]
			result_matrix.append(new_list)
		return result_matrix


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		n = len(Mat)
		res = [[] for i in range(n)]
		for i in range(n):
			if K > len(Mat[i]):
				a = K % len(Mat[i])
			else:
				a = K
			res[i] = Mat[i][a:] + Mat[i][:a]
		return res


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		if K > M:
			K = K % M
		result = []
		for i in range(N):
			k = K
			row = []
			for j in range(M):
				row.append(Mat[i][k])
				if k == M - 1:
					k = 0
				else:
					k += 1
			result.append(row)
		return result


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		ansMat = [[0 for j in range(M)] for i in range(N)]
		for i in range(N):
			for j in range(M):
				ansMat[i][j] = Mat[i][(j + K) % M]
		return ansMat


class Solution:

	def rotateMatrix(self, n, m, k, Mat):
		matrix2 = [[0 for j in range(m)] for i in range(n)]
		for i in range(n):
			for j in range(m):
				t = int((m + j - k % m) % m)
				matrix2[i][t] = Mat[i][j]
		return matrix2


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		k = K % M
		if k == 0:
			return Mat
		else:
			for i in range(N):
				li = Mat[i][k:] + Mat[i][0:k]
				Mat[i] = li
			return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for k in range(N):
			Mat[k].reverse()
			(i, j) = (0, M - K - 1)
			while i < j:
				(Mat[k][i], Mat[k][j]) = (Mat[k][j], Mat[k][i])
				i += 1
				j -= 1
			i = M - K
			j = M - 1
			while i < j:
				(Mat[k][i], Mat[k][j]) = (Mat[k][j], Mat[k][i])
				i += 1
				j -= 1
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		for ind in range(N):
			Mat[ind] = [Mat[ind][(i + K) % M] for (i, j) in enumerate(Mat[ind])]
		return Mat


def reverse(l, s, e):
	e -= 1
	while s < e:
		(l[s], l[e]) = (l[e], l[s])
		s += 1
		e -= 1

class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		K = K % M
		for i in range(N):
			l = Mat[i]
			reverse(l, 0, K)
			reverse(l, K, M)
			reverse(l, 0, M)
			Mat[i] = l
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		z = K % M
		for i in range(N):
			a = Mat[i][0:z][::-1]
			b = Mat[i][z:][::-1]
			Mat[i] = (a + b)[::-1]
		return Mat


class Solution:

	def rotateMatrix(self, N, M, K, Mat):
		TMat = list(zip(*Mat))
		TMat = TMat[K % M:] + TMat[:K % M]
		return list(zip(*TMat))