Given two square matrices matrixA and matrixB of size n x n.  Find the addition of these two matrices.
Note :- You don't have to return anything in this question (Refer YourTask section).
 
Example 1:
Input: matrixA = {{1, 2}, {3, 4}},
matrixB = {{4, 3}, {2, 1}}
Output: {{5, 5}, {5, 5}}
Example 1:
Input: matrixA = {{2, 2}, {2, 2}},
matrixB = {{3, 3}, {3, 3}}
Output: {{5, 5},{5, 5}}
 
Your Task:
You don't need to read or print anything. Your task is to complete the function Addition() which takes matrixA and matrixB as input parameters and adds two matrices. Do the addition without using extra memory. The output matrix must be in matrixA.
 
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

	def Addition(self, matrixA, matrixB):

		# Code here

Solutions:
class Solution:

	def Addition(self, matrixA, matrixB):
		n = len(matrixA)
		for i in range(n):
			for j in range(n):
				matrixA[i][j] += matrixB[i][j]
		return matrixA


class Solution:

	def Addition(self, matrixA, matrixB):
		rows = len(matrixA)
		for i in range(rows):
			cols = len(matrixA[i])
			for j in range(cols):
				matrixA[i][j] = matrixA[i][j] + matrixB[i][j]
		return matrixA


class Solution:

	def Addition(self, matrixA, matrixB):
		for i in range(len(matrixA)):
			for j in range(len(matrixA[0])):
				matrixA[i][j] += matrixB[i][j]


class Solution:

	def Addition(self, matrixA, matrixB):
		result = []
		row = []
		N = len(matrixA)
		for i in range(0, N):
			for j in range(0, N):
				matrixA[i][j] += matrixB[i][j]
		return


import numpy as np

class Solution:

	def Addition(self, matrixA, matrixB):
		for i in range(len(matrixA[0])):
			for j in range(len(matrixA[0])):
				matrixA[i][j] += matrixB[i][j]
		return matrixA


class Solution:

	def Addition(self, A, B):
		for i in range(len(A)):
			for j in range(len(A)):
				A[i][j] += B[i][j]


class Solution:

	def Addition(self, matrixA, matrixB):
		matrixC = []
		for i in range(len(matrixA)):
			row = []
			for j in range(len(matrixB)):
				matrixA[i][j] = matrixA[i][j] + matrixB[i][j]
		return


class Solution:

	def Addition(self, a, b):
		for i in range(len(a)):
			for j in range(len(a)):
				a[i][j] += b[i][j]
		return a


class Solution:

	def Addition(self, matrixA, matrixB):
		c = []
		for i in range(len(matrixA)):
			l = []
			for j in range(len(matrixB)):
				matrixA[i][j] += matrixB[i][j]
		return matrixA


class Solution:

	def Addition(self, X, Y):
		for i in range(len(X)):
			for j in range(len(Y)):
				X[i][j] += Y[i][j]


class Solution:

	def Addition(self, mA, mB):
		m1 = mA
		m2 = mB
		for i in range(len(mA)):
			for j in range(len(mA[0])):
				mA[i][j] = m1[i][j] + m2[i][j]


class Solution:

	def Addition(self, matrixA, matrixB):
		n = len(matrixA)
		m = len(matrixA[0])
		B = [[0] * m for _ in range(n)]
		for i in range(n):
			for j in range(m):
				matrixA[i][j] = matrixA[i][j] + matrixB[i][j]
		return B


class Solution:

	def Addition(self, matrixA, matrixB):
		r = [[0 for x in range(len(matrixA))] for y in range(len(matrixB))]
		for i in range(len(matrixA)):
			for j in range(len(matrixA[0])):
				matrixA[i][j] += matrixB[i][j]
		return matrixA


class Solution:

	def Addition(self, m1, m2):
		n = len(m1)
		for x in range(n):
			for y in range(n):
				m1[x][y] += m2[x][y]


n = 2

class Solution:

	def Addition(self, matrixA, matrixB):
		matrixC = matrixA[:][:]
		for n_row in range(n):
			for n_col in range(n):
				matrixC[n_row][n_col] = matrixA[n_row][n_col] + matrixB[n_row][n_col]
matrixA = [[1, 2], [3, 4]]
matrixB = [[4, 3], [2, 1]]
a = Solution()
a.Addition(matrixA, matrixB)
matrixA = [[2, 2], [2, 2]]
matrixB = [[3, 3], [3, 3]]
a = Solution()
a.Addition(matrixA, matrixB)