Given a square matrix of size n×n. Your task is to calculate the sum of its diagonals.
 
Example 1:
Input: matrix = {{1, 1, 1}, 
{1, 1, 1}, {1, 1, 1}}
Output: 6
Example 2:
Input: matrix = {{1, 2}, {3, 4}}
Output: 10
 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function DiagonalSum() which takes the matrix as input parameter and returns the sum of its diagonals.
 
Expected Time Complexity: O(n)
Expected Space Complexity: O(1)
Constraints:
1 <= n <= 100
1 <= matrix elements <= 10000

Difficulty: EASY
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
	def DiagonalSum(self, matrix):
		# Code here

Solutions:
class Solution:

	def DiagonalSum(self, matrix):
		s = 0
		n = len(matrix)
		for i in range(n):
			for j in range(n):
				if i == j:
					s += matrix[i][j]
				if i + j == n - 1:
					s += matrix[i][j]
		return s


class Solution:

	def DiagonalSum(self, matrix):
		matrix1 = 0
		for i in range(len(matrix[0])):
			for j in range(len(matrix[0])):
				if i == j:
					matrix1 += matrix[i][j]
				if i + j == len(matrix[0]) - 1:
					matrix1 += matrix[i][j]
		return matrix1


class Solution:

	def DiagonalSum(self, matrix):
		sum = 0
		for i in range(len(matrix)):
			sum += matrix[i][i] + matrix[i][len(matrix) - 1 - i]
		return sum


class Solution:

	def DiagonalSum(self, matrix):
		sum = 0
		rows = range(len(matrix))
		column_max = len(matrix[0]) - 1
		row_min = 0
		for i in rows:
			columns = range(len(matrix[i]))
			for j in columns:
				if i == j:
					sum = sum + matrix[i][j]
		for i in rows:
			columns = range(len(matrix[i]))
			for j in columns:
				if i == row_min and j == column_max:
					sum = sum + matrix[i][j]
			column_max = column_max - 1
			row_min = row_min + 1
		return sum


class Solution:

	def DiagonalSum(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		left_diagonal = 0
		right_diagonal = 0
		for i in range(row):
			for j in range(col):
				if i == j:
					left_diagonal += matrix[i][j]
				if i + j == col - 1:
					right_diagonal += matrix[i][j]
		return left_diagonal + right_diagonal


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix)
		(f, z) = (0, 0)
		for i in range(n):
			f += matrix[i][i]
			z += matrix[i][n - i - 1]
		return f + z


class Solution:

	def DiagonalSum(self, mat):
		s1 = 0
		s2 = 0
		for i in range(len(mat)):
			s1 += mat[i][i]
			s2 += mat[i][len(mat) - i - 1]
		return s1 + s2


class Solution:

	def DiagonalSum(self, matrix):
		res = 0
		n = len(matrix)
		for i in range(n):
			res += matrix[i][i]
			res += matrix[i][n - i - 1]
		return res


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix[0])
		diag = [0 for _ in range(2)]
		for i in range(n):
			diag[0] += matrix[i][i]
			diag[1] += matrix[i][n - i - 1]
		return sum(diag)


class Solution:

	def DiagonalSum(self, matrix):
		ans = 0
		n = len(matrix)
		for i in range(n):
			for j in range(n):
				if i - j == 0 or i + j == n - 1:
					ans += matrix[i][j]
		if n % 2 == 0:
			return ans
		else:
			return ans + matrix[n // 2][n // 2]


class Solution:

	def DiagonalSum(self, mat):
		n = len(mat)
		s = 0
		for i in range(n):
			s += mat[i][i]
		j = 0
		for i in range(n - 1, -1, -1):
			s += mat[j][i]
			j += 1
		return s


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix)
		(ps, ds) = (0, 0)
		for i in range(n):
			ps += matrix[i][i]
			ds += matrix[i][n - i - 1]
		return ps + ds


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix)
		r = []
		l = []
		for i in range(n):
			for j in range(n):
				if i == j:
					r.append(matrix[i][j])
		i = 0
		j = 0
		while i < n and j < n:
			l.append(matrix[i][n - j - 1])
			i += 1
			j += 1
		return sum(r) + sum(l)


class Solution:

	def DiagonalSum(self, matrix):
		d1 = 0
		for i in range(len(matrix)):
			d1 = d1 + matrix[i][i]
		d2 = 0
		for j in range(len(matrix)):
			d2 += matrix[j][len(matrix) - 1 - j]
		return d1 + d2


class Solution:

	def DiagonalSum(self, matrix):
		sum = 0
		for i in range(n):
			for j in range(n):
				if i == j:
					sum = sum + matrix[i][j]
				if i + j == n - 1:
					sum = sum + matrix[i][j]
		return sum


class Solution:

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


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix)
		sum_diag_p = 0
		sum_diag_opp = 0
		for i in range(n):
			for j in range(n):
				if i == j:
					sum_diag_p += matrix[i][j]
					sum_diag_opp += matrix[i][n - 1 - j]
		ans = sum_diag_p + sum_diag_opp
		return ans


class Solution:

	def DiagonalSum(self, matrix):
		s = 0
		for i in range(len(matrix)):
			for j in range(len(matrix[i])):
				if i == j:
					s += matrix[i][j]
		l = 0
		r = len(matrix[i]) - 1
		i = 0
		while i < len(matrix):
			s += matrix[l][r]
			l += 1
			r -= 1
			i += 1
		return s


class Solution:

	def DiagonalSum(self, matrix):
		sum1 = 0
		sum2 = 0
		for i in range(n):
			for j in range(n):
				if i == j:
					sum1 += matrix[i][j]
		for i in range(n):
			for j in range(n):
				if i + j == n - 1:
					sum2 += matrix[i][j]
		return sum1 + sum2


class Solution:

	def DiagonalSum(self, matrix):
		sumi = 0
		sumi1 = 0
		for i in range(len(matrix)):
			sumi += matrix[i][i]
			sumi1 += matrix[i][len(matrix) - i - 1]
		return sumi + sumi1


class Solution:

	def DiagonalSum(self, matrix):
		i = 0
		j = 0
		s_p = 0
		s_s = 0
		n = len(matrix)
		while i < n:
			s_p += matrix[i][j]
			s_s += matrix[i][n - j - 1]
			i += 1
			j += 1
		return s_p + s_s


class Solution:

	def DiagonalSum(self, matrix):
		n = len(matrix)
		(sum_l, sum_r) = (0, 0)
		for i in range(n):
			sum_l += matrix[i][i]
			sum_r += matrix[i][n - i - 1]
		return sum_l + sum_r


class Solution:

	def DiagonalSum(self, matrix):
		diag_sum = 0
		rows = len(matrix)
		cols = len(matrix[0])
		for i in range(rows):
			for j in range(cols):
				if i == j:
					diag_sum += matrix[i][j]
				if i + j == rows - 1:
					diag_sum += matrix[i][j]
		return diag_sum


class Solution:

	def DiagonalSum(self, matrix):
		import numpy as np
		matrix = np.array(matrix)
		return matrix.trace() + matrix[::-1].trace()


class Solution:

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


class Solution:

	def DiagonalSum(self, matrix):
		p_diag = 0
		diag = 0
		for i in range(len(matrix)):
			for j in range(len(matrix[i])):
				if i == j:
					p_diag += matrix[i][j]
		for i in matrix:
			i.reverse()
		for i in range(len(matrix)):
			for j in range(len(matrix[i])):
				if i == j:
					diag += matrix[i][j]
		sum = p_diag + diag
		return sum


class Solution:

	def DiagonalSum(self, matrix):
		total = 0
		for i in range(len(matrix)):
			total += matrix[i][i]
		j = len(matrix) - 1
		for i in range(len(matrix)):
			total += matrix[j][i]
			j -= 1
		return total


class Solution:

	def DiagonalSum(self, matrix):
		s = 0
		for i in range(n):
			s += matrix[i][i]
			s += matrix[i][n - i - 1]
		return s


class Solution:

	def DiagonalSum(self, matrix):
		dia1 = 0
		dia2 = 0
		n = len(matrix)
		for i in range(0, n):
			for j in range(n - 1 - (n - 1 - i), n, n):
				dia1 += matrix[i][j]
				dia2 += matrix[i][n - 1 - j]
		return dia1 + dia2


class Solution:

	def DiagonalSum(self, matrix):
		(diag1, diag2) = (0, 0)
		for i in range(len(matrix)):
			diag1 += matrix[i][i]
			diag2 += matrix[i][len(matrix) - i - 1]
		return diag1 + diag2


class Solution:

	def DiagonalSum(self, matrix):
		count = 0
		m = len(matrix)
		for i in range(m):
			count += matrix[i][i]
			count += matrix[i][-1 - i]
		return count


class Solution:

	def DiagonalSum(self, matrix):
		(x, y) = (0, 0)
		for i in range(len(matrix)):
			for j in range(len(matrix)):
				if i == j:
					x += matrix[i][j]
		(i, j) = (0, len(matrix) - 1)
		while i < len(matrix) and j >= 0:
			y += matrix[i][j]
			i += 1
			j -= 1
		return x + y


class Solution:

	def DiagonalSum(self, matrix):
		sumn = 0
		N = len(matrix[0])
		for i in range(N):
			sumn += matrix[i][i] + matrix[i][N - 1 - i]
		return sumn


class Solution:

	def DiagonalSum(self, matrix):
		s = 0
		for i in range(len(matrix)):
			for j in range(len(matrix)):
				if i == j:
					s += matrix[i][j]
					l = len(matrix) // 2
					if len(matrix) % 2 != 0 and l == i == j:
						s += matrix[i][j]
				elif len(matrix) - 1 - i == j:
					s += matrix[i][j]
		return s


class Solution:

	def DiagonalSum(self, matrix):
		l = len(matrix)
		ans = sum([matrix[x][x] + matrix[x][l - x - 1] for x in range(l)])
		return ans


class Solution:

	def DiagonalSum(self, matrix):
		mainDiagonal = 0
		secondaryDiagonal = 0
		for index in range(len(matrix)):
			mainDiagonal += matrix[index][index]
			secondaryDiagonal += matrix[index][len(matrix) - index - 1]
		return mainDiagonal + secondaryDiagonal


class Solution:

	def DiagonalSum(self, matrix):
		l = len(matrix)
		s = 0
		for i in range(l):
			s += matrix[i][i] + matrix[i][l - i - 1]
		return s


import numpy as np

class Solution:

	def DiagonalSum(self, matrix):
		sum = 0
		for i in range(len(matrix)):
			sum += matrix[i][i] + matrix[i][len(matrix) - 1 - i]
		return sum


class Solution:

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


class Solution:

	def DiagonalSum(self, matrix):
		prim = 0
		second = 0
		add = 0
		n = len(matrix)
		for i in range(len(matrix)):
			prim += matrix[i][i]
			second += matrix[i][n - i - 1]
		add = prim + second
		return add


class Solution:

	def DiagonalSum(self, matrix):
		matrix_len = len(matrix)
		row_len = len(matrix[0])
		reversed_matrix = matrix[::-1]
		sum = 0
		for i in range(matrix_len):
			for j in range(row_len):
				if i == j:
					sum += matrix[i][j]
		for i in range(matrix_len):
			for j in range(row_len):
				if i == j:
					sum += reversed_matrix[i][j]
		return sum


class Solution:

	def DiagonalSum(self, matrix):
		sm = 0
		for x in range(n):
			sm += matrix[x][x]
			sm += matrix[x][n - x - 1]
		return sm