Given a square matrix of size N X N, turn it by 180 degrees in anticlockwise direction without using extra memory.
 
Example 1:
Input: N = 4, 
matrix = {{1, 2, 3, 4}, 
          {5, 6, 7 ,8}, 
          {9, 10, 11, 12},
          {13, 14, 15, 16}}
Output: {{16, 15, 14, 13}, 
         {12, 11, 10, 9}, 
         {8, 7, 6, 5}, 
         {4, 3, 2, 1}}
Example 2:
Input: N = 2,
matrix = {{1, 2},
          {3, 4}}
Output: {{4, 3}, 
         {2, 1}}
 
Your Task:
You don't need to read or print anything. Your task is to complete the function rotate() which takes matrix as input parameter and rotate the matrix by 180 degree wihtout using extraa memory. You have to rotate the matrix in-place which means you have to modify the input matrix directly.
 
Expected Time Complexity: O(N^{2})
Expected Space Complexity: O(1)
 
Constraints:
1 ≤ N ≤ 500

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

	def rotate(self, matrix):

		# Code here

Solutions:
class Solution:

	def rotate(self, matrix):
		arr = []
		n = len(matrix) - 1
		for item in matrix:
			item.reverse()
			arr.insert(0, item)
		for (i, item) in enumerate(arr):
			matrix[i] = item


class Solution:

	def rotate(self, matrix):
		for i in range(n):
			for j in range(n // 2):
				(matrix[i][j], matrix[i][n - 1 - j]) = (matrix[i][n - 1 - j], matrix[i][j])
		for i in range(n // 2):
			for j in range(n):
				(matrix[i][j], matrix[n - 1 - i][j]) = (matrix[n - 1 - i][j], matrix[i][j])
		return matrix


class Solution:

	def rotate(self, matrix):
		ans = []
		for i in matrix[::-1]:
			ans.append(i[::-1])
		n = len(ans)
		for i in range(n):
			for j in range(n):
				matrix[i][j] = ans[i][j]


class Solution:

	def rotate(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		for i in range(row):
			(j, k) = (0, col - 1)
			while j < k:
				(matrix[i][j], matrix[i][k]) = (matrix[i][k], matrix[i][j])
				j += 1
				k -= 1
		for i in range(col):
			(j, k) = (0, row - 1)
			while j < k:
				(matrix[j][i], matrix[k][i]) = (matrix[k][i], matrix[j][i])
				j += 1


class Solution:

	def rotate(self, matrix):
		s = 0
		e = len(matrix) - 1
		t = len(matrix) - 1
		while s < e:
			for i in range(len(matrix)):
				(matrix[s][i], matrix[e][t - i]) = (matrix[e][t - i], matrix[s][i])
			s += 1
			e -= 1
		if len(matrix) % 2 == 1:
			for i in range(len(matrix) // 2):
				(matrix[s][i], matrix[e][t - i]) = (matrix[e][t - i], matrix[s][i])
		return matrix


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		for i in range(n):
			start = 0
			end = n - 1
			while start < end:
				(matrix[i][start], matrix[i][end]) = (matrix[i][end], matrix[i][start])
				start += 1
				end -= 1
		for j in range(n):
			start = 0
			end = n - 1
			while start < end:
				(matrix[start][j], matrix[end][j]) = (matrix[end][j], matrix[start][j])
				start += 1
				end -= 1
		return matrix


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			matrix[i] = matrix[i][::-1]
		for i in range(len(matrix) // 2):
			(matrix[i], matrix[len(matrix) - i - 1]) = (matrix[len(matrix) - i - 1], matrix[i])


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		r = n // 2
		for i in range(r):
			for j in range(n):
				(matrix[i][j], matrix[n - i - 1][j]) = (matrix[n - i - 1][j], matrix[i][j])
		for i in range(n):
			for j in range(r):
				(matrix[i][j], matrix[i][n - j - 1]) = (matrix[i][n - j - 1], matrix[i][j])


class Solution:

	def rotate(self, mat):
		n = len(mat)
		m = len(mat[0])
		i = 0
		r1 = 0
		r2 = n - 1
		j = m - 1
		while r1 < r2:
			while i < m and j >= 0:
				(mat[r1][i], mat[r2][j]) = (mat[r2][j], mat[r1][i])
				i += 1
				j -= 1
			r1 += 1
			r2 -= 1
			i = 0
			j = m - 1
		if r1 == r2:
			while i < j:
				(mat[r1][i], mat[r2][j]) = (mat[r2][j], mat[r1][i])
				i += 1
				j -= 1


class Solution:

	def rotate(self, matrix):
		for i in range(n // 2):
			for j in range(n):
				temp = matrix[i][j]
				matrix[i][j] = matrix[n - 1 - i][n - 1 - j]
				matrix[n - 1 - i][n - 1 - j] = temp
		if n % 2 != 0:
			i = n // 2
			for j in range(n // 2):
				temp = matrix[i][j]
				matrix[i][j] = matrix[n - 1 - i][n - 1 - j]
				matrix[n - 1 - i][n - 1 - j] = temp
		return matrix


class Solution:

	def rotate(self, matrix):
		l = len(matrix)
		for i in range(n):
			ll = matrix[i]
			ll.reverse()
			matrix[i] = ll
		i = 0
		m = n - 1
		while i < m:
			temp = matrix[i]
			matrix[i] = matrix[m]
			matrix[m] = temp
			i = i + 1
			m = m - 1


class Solution:

	def rotate(self, matrix):
		c = []
		n = len(matrix)
		m = len(matrix[0])
		for i in range(n):
			for j in range(m):
				c.append(matrix[i][j])
		k = 0
		c = c[::-1]
		for i in range(n):
			for j in range(m):
				matrix[i][j] = c[k]
				k += 1
		return matrix


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			for j in range(len(matrix[i]) // 2):
				temp = matrix[i][j]
				matrix[i][j] = matrix[i][len(matrix) - j - 1]
				matrix[i][len(matrix) - j - 1] = temp
		for i in range(len(matrix) // 2):
			for j in range(len(matrix[i])):
				temp = matrix[i][j]
				matrix[i][j] = matrix[len(matrix[i]) - i - 1][j]
				matrix[len(matrix[i]) - i - 1][j] = temp
		return matrix


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			matrix[i] = matrix[i][::-1]
		t = []
		l = len(matrix)
		for i in range(l - 1, -1, -1):
			t.append(matrix[i])
		for i in range(l):
			matrix[i] = t[i]


class Solution:

	def reverse_arr(self, row, size):
		pointer_one = 0
		pointer_two = size
		while pointer_one < pointer_two:
			(row[pointer_one], row[pointer_two]) = (row[pointer_two], row[pointer_one])
			pointer_one += 1
			pointer_two -= 1
		return row

	def rotate(self, matrix):
		size = len(matrix)
		for i in range(size):
			matrix[i] = self.reverse_arr(matrix[i], size - 1)
		for j in range(size // 2):
			for k in range(size):
				(matrix[j][k], matrix[size - j - 1][k]) = (matrix[size - j - 1][k], matrix[j][k])
		return matrix


class Solution:

	def rotate(self, mat):
		n = len(mat)
		i = 0
		while i < n:
			j = 0
			y = n - 1
			while j < y:
				(mat[i][j], mat[i][y]) = (mat[i][y], mat[i][j])
				j += 1
				y -= 1
			i += 1
		j = 0
		while j < n:
			i = 0
			x = n - 1
			while i < x:
				(mat[i][j], mat[x][j]) = (mat[x][j], mat[i][j])
				i += 1
				x -= 1
			j += 1


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		for i in range(n):
			for j in range(n // 2):
				temp = matrix[i][j]
				matrix[i][j] = matrix[i][n - 1 - j]
				matrix[i][n - 1 - j] = temp
		for i in range(n // 2):
			for j in range(n):
				temp = matrix[i][j]
				matrix[i][j] = matrix[n - 1 - i][j]
				matrix[n - 1 - i][j] = temp
		return matrix
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		matrix = []
		for _ in range(n):
			matrix.append(list(map(int, input().split())))
		ob = Solution()
		ob.rotate(matrix)
		for i in range(n):
			for j in range(n):
				print(matrix[i][j], end=' ')
			print()


class Solution:

	def rotate(self, matrix):
		count = 0
		mat = matrix
		N = len(mat)
		while True:
			if count >= 2:
				break
			for i in range(N // 2):
				for j in range(i, N - i - 1):
					temp = mat[i][j]
					mat[i][j] = mat[j][N - i - 1]
					mat[j][N - i - 1] = mat[N - i - 1][N - j - 1]
					mat[N - i - 1][N - j - 1] = mat[N - j - 1][i]
					mat[N - j - 1][i] = temp
			count += 1
		return mat
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		matrix = []
		for _ in range(n):
			matrix.append(list(map(int, input().split())))
		ob = Solution()
		ob.rotate(matrix)
		for i in range(n):
			for j in range(n):
				print(matrix[i][j], end=' ')
			print()


class Solution:

	def reverseColumns(self, arr):
		C = len(arr[0])
		for i in range(C):
			j = 0
			k = C - 1
			while j < k:
				t = arr[j][i]
				arr[j][i] = arr[k][i]
				arr[k][i] = t
				j += 1
				k -= 1

	def transpose(self, arr):
		R = len(arr)
		C = len(arr[0])
		for i in range(R):
			for j in range(i, C):
				t = arr[i][j]
				arr[i][j] = arr[j][i]
				arr[j][i] = t

	def rotate(self, matrix):
		self.transpose(matrix)
		self.reverseColumns(matrix)
		self.transpose(matrix)
		self.reverseColumns(matrix)
		return matrix


class Solution:

	def rotate(self, matrix):
		l = 0
		m = len(matrix) - 1
		while l < m:
			(matrix[l], matrix[m]) = (matrix[m], matrix[l])
			l += 1
			m -= 1
		for i in range(len(matrix)):
			matrix[i] = matrix[i][::-1]
		return matrix


class Solution:

	def rotate(self, mat):
		c = len(mat[0])
		for i in range(c * c // 2):
			(mat[c - 1 - i // c][c - 1 - i % c], mat[i // c][i % c]) = (mat[i // c][i % c], mat[c - 1 - i // c][c - 1 - i % c])
		return mat


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			matrix[i] = matrix[i][::-1]
		j = len(matrix[0]) - 1
		i = 0
		while i != j and i < j:
			(matrix[i], matrix[j]) = (matrix[j], matrix[i])
			i += 1
			j -= 1
		return matrix


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		if n % 2 != 0:
			for i in range(int((n + 1) / 2)):
				c = n - 1
				for j in range(n):
					if i == int((n - 1) / 2):
						if j <= int((n - 1) / 2):
							temp = matrix[i][j]
							matrix[i][j] = matrix[n - i - 1][c]
							matrix[n - i - 1][c] = temp
							c -= 1
						else:
							continue
					else:
						temp = matrix[i][j]
						matrix[i][j] = matrix[n - i - 1][c]
						matrix[n - i - 1][c] = temp
						c -= 1
		else:
			for i in range(int((n + 1) / 2)):
				c = n - 1
				for j in range(n):
					temp = matrix[i][j]
					matrix[i][j] = matrix[n - i - 1][c]
					matrix[n - i - 1][c] = temp
					c -= 1


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			matrix[i].reverse()
		matrix.reverse()
		return matrix


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		for i in range(0, n):
			for j in range(0, i):
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
		for i in range(0, n):
			for j in range(0, n - i):
				(matrix[i][j], matrix[n - 1 - j][n - 1 - i]) = (matrix[n - 1 - j][n - 1 - i], matrix[i][j])


class Solution:

	def rotate(self, matrix):
		for i in range(len(matrix)):
			for j in range(len(matrix[0]) // 2):
				(matrix[i][j], matrix[i][-j - 1]) = (matrix[i][-j - 1], matrix[i][j])
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if i <= j:
					(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
		for i in range(len(matrix)):
			for j in range(len(matrix[0]) // 2):
				(matrix[i][j], matrix[i][-j - 1]) = (matrix[i][-j - 1], matrix[i][j])
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if i <= j:
					(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
		return matrix


class Solution:

	def rotate(self, matrix):
		l = len(matrix)
		if l == 1:
			return matrix
		for i in range(l // 2):
			matrix[i].reverse()
			matrix[n - 1 - i].reverse()
			temp = list(matrix[i])
			li1 = list(matrix[n - 1 - i])
			matrix[i] = li1
			matrix[n - 1 - i] = temp
		if l % 2 != 0:
			matrix[l // 2].reverse()
		return matrix


class Solution:

	def rotate(self, matrix):
		matrix.reverse()
		for i in matrix:
			i.reverse()


class Solution:

	def rotate(self, matrix):
		matrix[:] = list(map(list, zip(*matrix[::-1])))
		matrix[:] = list(map(list, zip(*matrix[::-1])))


class Solution:

	def rotate(self, matrix):
		n = len(matrix)
		j = n - 1
		for i in range(n):
			if i <= j:
				temp = matrix[i]
				matrix[i] = matrix[j]
				matrix[j] = temp
				j -= 1
		for i in range(n):
			j = 0
			k = n - 1
			while j <= k:
				temp = matrix[i][j]
				matrix[i][j] = matrix[i][k]
				matrix[i][k] = temp
				j += 1
				k -= 1
		return matrix