Given a square matrix of size N x N. The task is to rotate it by 90 degrees in anti-clockwise direction without using any extra space. 
Example 1:
Input:
N = 3 
matrix[][] = {{1, 2, 3},
              {4, 5, 6}
              {7, 8, 9}}
Output: 
Rotated Matrix:
3 6 9
2 5 8
1 4 7
Example 2:
Input:
N = 2
matrix[][] = {{1, 2},
              {3, 4}}
Output: 
Rotated Matrix:
2 4
1 3
Your Task:
You dont need to read input or print anything. Complete the function rotateby90() which takes the matrix as input parameter and rotates it by 90 degrees in anti-clockwise direction without using any extra space. You have to modify the input matrix in-place. 
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 100
1 <= matrix[][] <= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3





class Solution:

    

    #Function to rotate matrix anticlockwise by 90 degrees.

    def rotateby90(self,a, n): 

        # code here

Solutions:
class Solution:

	def rotateby90(self, a, n):
		for i in range(n // 2):
			for j in range(i, n - i - 1):
				temp = a[i][j]
				a[i][j] = a[j][n - i - 1]
				a[j][n - i - 1] = a[n - i - 1][n - j - 1]
				a[n - i - 1][n - j - 1] = a[n - j - 1][i]
				a[n - j - 1][i] = temp


class Solution:

	def rotateby90(self, a, n):

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


class Solution:

	def rotateby90(self, a, n):
		p = [[matrix[i][j] for j in range(n)] for i in range(n)]
		k = 0
		for i in range(n):
			for j in range(n):
				matrix[i][j] = p[j][n - 1 - k]
			k += 1


class Solution:

	def rotateby90(self, a, n):
		u = []
		for i in range(n):
			v = []
			for j in range(n):
				v.append(a[j][i])
			u.append(v)
		u = u[::-1]
		for j in range(len(u)):
			a[j] = u[j]


class Solution:

	def rotateby90(self, a, n):
		lst = []
		for i in zip(*matrix):
			lst = [i] + lst
		matrix[:] = lst[:]


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			a[i].reverse()
		for i in range(n):
			for j in range(i, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		return a


import copy

class Solution:

	def rotateby90(self, a, n):
		c_arr = copy.deepcopy(a)
		(r, c) = (0, n - 1)
		(i, j) = (0, 0)
		while c >= 0:
			while r < n:
				a[i][j] = c_arr[r][c]
				r += 1
				j += 1
			c -= 1
			i += 1
			r = 0
			j = 0


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			for j in range(i, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		p1 = 0
		p2 = n - 1
		while p1 <= p2:
			(a[p1], a[p2]) = (a[p2], a[p1])
			p1 += 1
			p2 -= 1
		return a


class Solution:

	def rotateby90(self, a, n):
		m = []
		for i in reversed(range(n)):
			p = []
			for j in range(n):
				p.append(matrix[j][i])
			m.append(p)
		for i in range(n):
			for j in range(n):
				matrix[i][j] = m[i][j]


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			for j in range(i + 1, n):
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
		for i in range(n):
			low = 0
			high = n - 1
			while low < high:
				(matrix[low][i], matrix[high][i]) = (matrix[high][i], matrix[low][i])
				high -= 1
				low += 1


class Solution:

	def rotateby90(self, a, n):
		list = []
		for i in range(n - 1, -1, -1):
			for j in range(n):
				list.append(a[j][i])
		k = 0
		for i in range(n):
			for j in range(n):
				a[i][j] = list[k]
				k = k + 1
		return a


class Solution:

	def rotateby90(self, a, n):
		for i in range(n - 1):
			for j in range(i + 1, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		matrix.reverse()


class Solution:

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


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			for j in range(i + 1, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		l = 0
		h = n - 1
		while l < h:
			for j in range(n):
				(a[l][j], a[h][j]) = (a[h][j], a[l][j])
			l += 1
			h -= 1


class Solution:

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


class Solution:

	def rotateby90(self, a, n):
		for i in range(len(matrix)):
			for j in range(i, len(matrix)):
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
		matrix[:] = matrix[::-1]
		return matrix


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			for j in range(n):
				if i > j:
					(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		top = 0
		bottom = n - 1
		while top < bottom:
			for j in range(n):
				(a[top][j], a[bottom][j]) = (a[bottom][j], a[top][j])
			top += 1
			bottom -= 1
		return a


class Solution:

	def rotateby90(self, a, n):
		start = 0
		end = n - 1
		while start < end:
			for i in range(n):
				t = a[i][start]
				a[i][start] = a[i][end]
				a[i][end] = t
			start += 1
			end -= 1
		for i in range(n):
			for j in range(i + 1, n):
				t = a[i][j]
				a[i][j] = a[j][i]
				a[j][i] = t


class Solution:

	def rotateby90(self, a, n):
		n = len(a)
		ans = [[0] * n for _ in range(n)]
		for i in range(n):
			for j in range(n):
				ans[j][n - i - 1] = a[i][j]
		for i in range(n):
			for j in range(n):
				a[n - i - 1][n - j - 1] = ans[i][j]
		return a


class Solution:

	def rotateClock(self, arr, n):
		for i in range(n):
			arr[i].reverse()
		for i in range(n):
			for j in range(i + 1, n):
				(arr[i][j], arr[j][i]) = (arr[j][i], arr[i][j])
		return arr

	def rotateby90(self, a, n):
		a = self.rotateClock(a, n)


class Solution:

	def rotateby90(self, a, n):
		temp = []
		for i in range(n - 1, -1, -1):
			li = []
			for j in range(n):
				li.append(a[j][i])
			temp.append(li)
		for i in range(n):
			for j in range(n):
				a[i][j] = temp[i][j]


import numpy as np

class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			a[i] = a[i][::-1]
		for i in range(n):
			for j in range(i):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			for j in range(i + 1, n):
				temp = a[i][j]
				a[i][j] = a[j][i]
				a[j][i] = temp
		(left, right) = (0, n - 1)
		while left < right:
			for j in range(n):
				temp = a[left][j]
				a[left][j] = a[right][j]
				a[right][j] = temp
			left += 1
			right -= 1


class Solution(object):

	def rotateby90(self, matrix, n):
		for row in range(n):
			(left, right) = (0, n - 1)
			while left < right:
				(matrix[row][left], matrix[row][right]) = (matrix[row][right], matrix[row][left])
				left += 1
				right -= 1
		for row in range(n):
			for col in range(row):
				(matrix[row][col], matrix[col][row]) = (matrix[col][row], matrix[row][col])
		return matrix


class Solution:

	def reverse(self, m):
		i = 0
		j = len(m) - 1
		while i <= j:
			(m[i], m[j]) = (m[j], m[i])
			i += 1
			j -= 1

	def rotateby90(self, matrix, n):
		for i in range(n):
			self.reverse(matrix[i])
		for i in range(n):
			for j in range(i, n):
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


class Solution:

	def rotateby90(self, a, n):
		x = 0
		y = n - 1
		while x < y:
			for i in range(n):
				(a[i][x], a[i][y]) = (a[i][y], a[i][x])
			x += 1
			y -= 1
		for i in range(n):
			for j in range(i, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])
		return a


class Solution:

	def rotateby90(self, a, n):
		for i in range(n // 2):
			col = n - 1 - i
			for j in range(i, col):
				(dx, dy) = (j - i, col - j)
				(a[i][j], a[i + dx][j + dy], a[i + dx + dy][j + dy - dx], a[i + dy][j - dx]) = (a[i + dx][j + dy], a[i + dx + dy][j + dy - dx], a[i + dy][j - dx], a[i][j])


class Solution:

	def rotateby90(self, a, n):
		l = []
		for i in range(-1, -(len(a) + 1), -1):
			k = []
			for j in range(len(a)):
				k.append(a[j][i])
			l.append(k)
		for i in range(len(l)):
			for j in range(len(l)):
				a[i][j] = l[i][j]


class Solution:

	def rotateby90(self, a, n):
		rotated = [[0 for _ in range(n)] for _ in range(n)]
		for i in range(n):
			for j in range(n):
				rotated[i][j] = a[j][n - i - 1]
		a[:] = rotated[:]


class Solution:

	def rotateby90(self, a, n):
		temp = [[0] * n for i in range(n)]
		for i in range(n):
			for j in range(n):
				temp[n - j - 1][i] = a[i][j]
		for r in range(n):
			for c in range(len(temp[0])):
				a[r][c] = temp[r][c]
		return a


class Solution:

	def rotateby90(self, a, n):
		(r, c) = (len(a), len(a[0]))
		l = [[0] * c for i in range(r)]
		for i in range(c - 1, -1, -1):
			f = 0
			for j in range(r):
				l[i][j] = a[j][i]
				f += 1
			f = 0
		res = l[::-1]
		for i in range(r):
			for j in range(c):
				a[i][j] = res[i][j]
		return a


class Solution:

	def rotateby90(self, a, n):
		pointer1 = 0
		pointer2 = n - 1
		while pointer1 < pointer2:
			for i in range(n):
				(a[i][pointer1], a[i][pointer2]) = (a[i][pointer2], a[i][pointer1])
			pointer1 = pointer1 + 1
			pointer2 = pointer2 - 1
		for i in range(n):
			for j in range(i, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])


class Solution:

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


import copy
import numpy

class Solution:

	def rotateby90(self, a, n):
		a1 = copy.deepcopy(a)
		d = numpy.shape(a)
		a.clear()
		for i in range(d[1]):
			arr = []
			for j in range(d[0]):
				arr.append(a1[j][i])
			a.append(arr)
		a.reverse()


class Solution:

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


class Solution:

	def rotateby90(self, a, n):
		for r in range(n):
			for c in range(r + 1, n):
				(a[r][c], a[c][r]) = (a[c][r], a[r][c])
		for r in range(n):
			l = 0
			h = n - 1
			while l < h:
				(a[l][r], a[h][r]) = (a[h][r], a[l][r])
				l += 1
				h -= 1


class Solution:

	def rotateby90(self, matrix, n):
		(l, r) = (0, n - 1)
		while l < r:
			for i in range(r - l):
				(top, bottom) = (l, r)
				topright = matrix[top][r - i]
				matrix[top][r - i] = matrix[bottom - i][r]
				matrix[bottom - i][r] = matrix[bottom][l + i]
				matrix[bottom][l + i] = matrix[top + i][l]
				matrix[top + i][l] = topright
			r -= 1
			l += 1


class Solution:

	def rotateby90(self, mat, n):
		for i in range(len(mat)):
			mat[i].reverse()
		for i in range(len(mat)):
			for j in range(i + 1, len(mat)):
				(mat[i][j], mat[j][i]) = (mat[j][i], mat[i][j])


class Solution:

	def rotateby90(self, a, n):
		for (r, i) in enumerate(tuple(zip(*a))[::-1]):
			for (c, j) in enumerate(i):
				a[r][c] = j


class Solution:

	def rotateby90(self, a, n):
		(n, m) = (len(matrix), len(matrix[0]))
		for raw in matrix:
			(i, j) = (0, len(raw) - 1)
			while i < j:
				(raw[i], raw[j]) = (raw[j], raw[i])
				i += 1
				j -= 1
		for i in range(n):
			for j in range(i + 1, m):
				if i != j:
					(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


import numpy as np

class Solution:

	def rotateby90(self, a, n):
		pointer1 = 0
		pointer2 = n - 1
		while pointer1 < pointer2:
			for i in range(n):
				(a[i][pointer1], a[i][pointer2]) = (a[i][pointer2], a[i][pointer1])
			pointer1 = pointer1 + 1
			pointer2 = pointer2 - 1
		for i in range(n):
			for j in range(i, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])


class Solution:

	def rotateby90(self, a, n):
		mat = [[0 for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				mat[n - j - 1][i] = a[i][j]
		for i in range(n):
			for j in range(n):
				a[i][j] = mat[i][j]


class Solution:

	def rotateby90(self, arr, n):
		for i in range(n):
			for j in range(n):
				if j >= i:
					(arr[i][j], arr[j][i]) = (arr[j][i], arr[i][j])
		for i in range(n // 2):
			for j in range(n):
				(arr[i][j], arr[n - 1 - i][j]) = (arr[n - 1 - i][j], arr[i][j])
		return arr


class Solution:

	def rotateby90(self, a, n):
		for i in a:
			k = i[::-1]
			for j in range(n):
				i[j] = k[j]
		for i in range(n):
			for j in range(i):
				(a[j][i], a[i][j]) = (a[i][j], a[j][i])


class Solution:

	def rotateby90(self, a, n):
		for i in range(n):
			x = a[i]
			x[:] = reversed(x[:])
		for i in range(n):
			for j in range(i + 1, n):
				(a[i][j], a[j][i]) = (a[j][i], a[i][j])


class Solution:

	def rotateby90(self, matrix, n):
		p1 = 0
		p2 = n - 1
		while p1 < p2:
			for i in range(n):
				(matrix[i][p1], matrix[i][p2]) = (matrix[i][p2], matrix[i][p1])
			p1 += 1
			p2 -= 1
		for i in range(n):
			for j in range(i):
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


import numpy as np

class Solution:

	def rotateby90(self, a, n):
		brr = np.array(a)
		brr = brr.flatten()
		brr = brr[::-1]
		arr = []
		x = len(brr)
		j = x - n
		crr = []
		while j < x:
			i = j
			while i >= 0:
				arr.append(brr[i])
				i -= n
			j += 1
		k = 0
		for m in range(0, n):
			for r in range(0, n):
				a[m][r] = arr[k]
				k += 1
		return a