Given a square matrix of size N*N, print the sum of upper and lower triangular elements. Upper Triangle consists of elements on the diagonal and above it. The lower triangle consists of elements on the diagonal and below it. 
Example 1:
Input:
N = 3 
mat[][] = {{6, 5, 4},
           {1, 2, 5}
           {7, 9, 7}}
Output: 29 32
Explanation:
The given matrix is
6 5 4
1 2 5
7 9 7
The elements of upper triangle are
6 5 4
  2 5
    7
Sum of these elements is 6+5+4+2+5+7=29
The elements of lower triangle are
6
1 2
7 9 7
Sum of these elements is 6+1+2+7+9+7= 32.
Example 2:
Input:
N = 2
mat[][] = {{1, 2},
           {3, 4}}
Output: 7 8
Explanation:
Upper triangular matrix:
1 2
  4
Sum of these elements are 7.
Lower triangular matrix:
1
3 4
Sum of these elements are 8.
Your Task:
You don't need to read input or print anything. Complete the function sumTriangles() that takes matrix and its size N as input parameters and returns the list of integers containing the sum of upper and lower triangle. 
Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(1)
Constraints: 
1 <= N <= 100
1 <= matrix[i][j] <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: 
#User function Template for python3


class Solution:
    
    #Function to return sum of upper and lower triangles of a matrix.
    def sumTriangles(self,matrix, n):
        # code here 

Solutions:
class Solution:

	def sumTriangles(self, matrix, n):
		(upper, lower) = (0, 0)
		for i in range(n):
			for j in range(n):
				if i >= j:
					lower += matrix[i][j]
				if i <= j:
					upper += matrix[i][j]
		return [upper, lower]


class Solution:

	def sumTriangles(self, matrix, n):
		R = len(matrix)
		C = len(matrix[0])
		i = 0
		j = 0
		upper_sum = 0
		lower_sum = 0
		for i in range(R):
			for j in range(C):
				if i <= j:
					upper_sum += matrix[i][j]
		for i in range(R):
			for j in range(C):
				if i >= j:
					lower_sum += matrix[i][j]
		return (upper_sum, lower_sum)


class Solution:

	def sumTriangles(self, matrix, n):
		c = 0
		d = 0
		l = []
		l1 = []
		for i in range(n):
			for j in range(n):
				if i <= j:
					c += matrix[i][j]
				if i >= j:
					d += matrix[i][j]
		return (c, d)


class Solution:

	def sumTriangles(self, matrix, n):
		sum = 0
		ans = [None] * 2
		for i in range(n):
			for j in range(n):
				if i == j:
					sum += matrix[i][j]
		sum1 = sum
		sum2 = sum
		for i in range(n):
			for j in range(i + 1, n):
				sum1 += matrix[i][j]
		ans[0] = sum1
		for i in range(n):
			for j in range(i + 1, n):
				sum2 += matrix[j][i]
		ans[1] = sum2
		return ans


class Solution:

	def sumTriangles(self, m, n):
		(a, b) = ([], [])
		for i in range(n):
			for j in range(n):
				if i < j or i == j:
					a.append(m[i][j])
				if i > j or i == j:
					b.append(m[i][j])
		return (sum(a), sum(b))


class Solution:

	def sumTriangles(self, matrix, n):
		u = 0
		l = 0
		for i in range(n):
			u = u + sum(matrix[i][i:])
			l = l + sum(matrix[i][:i + 1])
		return (u, l)


class Solution:

	def sumTriangles(self, matrix, n):
		upper_sum = 0
		for row in range(n):
			for col in range(row, n):
				upper_sum += matrix[row][col]
		lower_sum = 0
		for row in range(n):
			for col in range(row + 1):
				lower_sum += matrix[row][col]
		res = []
		res.append(upper_sum)
		res.append(lower_sum)
		return res


class Solution:

	def sumTriangles(self, matrix, n):
		n1 = len(matrix)
		m1 = len(matrix[0])
		upper_sum = 0
		for i in range(n1):
			for j in range(i, n1):
				upper_sum += matrix[i][j]
		lower_sum = 0
		for i in range(n1):
			for j in range(i + 1):
				lower_sum += matrix[i][j]
		res = []
		res.append(upper_sum)
		res.append(lower_sum)
		return res


class Solution:

	def sumTriangles(self, matrix, n):
		us = 0
		ls = 0
		for i in range(n):
			for j in range(i, n):
				us += matrix[i][j]
			for j in range(0, i + 1):
				ls += matrix[i][j]
		return (us, ls)


class Solution:

	def sumTriangles(self, matrix, n):
		(uS, lS) = (0, 0)
		for i in range(n):
			for j in range(i, n):
				uS += matrix[i][j]
			for j in range(0, i + 1):
				lS += matrix[i][j]
		return (uS, lS)


class Solution:

	def sumTriangles(self, matrix, n):
		upper = 0
		lower = 0
		for r in range(n):
			for c in range(r + 1):
				lower += matrix[r][c]
			for c in range(r, n):
				upper += matrix[r][c]
		return (upper, lower)


class Solution:

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


class Solution:

	def sumTriangles(self, matrix, n):
		u = []
		l = []
		for i in range(n):
			for j in range(i, n):
				u.append(matrix[i][j])
				l.append(matrix[j][i])
		return [sum(u), sum(l)]


class Solution:

	def sumTriangles(self, matrix, n):
		li = matrix
		row = n
		col = n
		sum_upper = 0
		sum_lower = 0
		for i in range(row):
			for j in range(col):
				if i > j:
					sum_lower += li[i][j]
				elif j > i:
					sum_upper += li[i][j]
				else:
					sum_lower += li[i][j]
					sum_upper += li[i][j]
		return (sum_upper, sum_lower)


class Solution:

	def sumTriangles(self, matrix, n):
		l = []
		sum1 = 0
		c1 = 0
		sum2 = 0
		c2 = n - 1
		for i in range(n):
			for j in range(c1, n):
				sum1 += matrix[i][j]
			c1 += 1
		l.append(sum1)
		for i in range(n - 1, -1, -1):
			for j in range(c2, -1, -1):
				sum2 += matrix[i][j]
			c2 -= 1
		l.append(sum2)
		return l


class Solution:

	def sumTriangles(self, matrix, n):
		sumup = 0
		sumdo = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					sumup += matrix[i][j]
				if j <= i:
					sumdo += matrix[i][j]
		return (sumup, sumdo)


class Solution:

	def sumTriangles(self, matrix, n):
		a = 0
		b = 0
		for i in range(n):
			for j in range(n):
				if j >= i:
					a += matrix[i][j]
				if i >= j:
					b += matrix[i][j]
		return [a, b]


class Solution:

	def sumTriangles(self, matrix, n):
		upperTriangleSum = 0
		lowerTriangleSum = 0
		for row in range(n):
			for column in range(row, n):
				upperTriangleSum += matrix[row][column]
		for row in range(n):
			for column in range(row + 1):
				lowerTriangleSum += matrix[row][column]
		return (upperTriangleSum, lowerTriangleSum)


class Solution:

	def sumTriangles(self, matrix, n):
		sum1 = 0
		for i in range(n):
			for j in range(n):
				if i >= j:
					sum1 += matrix[i][j]
		sum2 = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					sum2 += matrix[i][j]
		return (sum2, sum1)


class Solution:

	def sumTriangles(self, matrix, n):
		(upper, lower) = (0, 0)
		for i in range(len(matrix)):
			for j in range(i, len(matrix[0])):
				upper += matrix[i][j]
		for i in range(len(matrix)):
			for j in range(0, i + 1):
				lower += matrix[i][j]
		return [upper, lower]


class Solution:

	def sumTriangles(self, matrix, n):
		sum_upper = 0
		sum_lower = 0
		for i in range(n):
			for j in range(n):
				if j >= i:
					sum_upper += matrix[i][j]
				if i >= j:
					sum_lower += matrix[i][j]
		return [sum_upper, sum_lower]


class Solution:

	def sumTriangles(self, matrix, n):
		upsum = 0
		for i in range(n):
			for j in range(i, n):
				upsum += matrix[i][j]
		lowsum = 0
		for i in range(n):
			for j in range(i + 1):
				lowsum += matrix[i][j]
		return (upsum, lowsum)


class Solution:

	def sumTriangles(self, matrix, n):
		(d, e) = (0, 0)
		for i in range(n):
			for j in range(i + 1):
				d = d + matrix[i][j]
				e = e + matrix[j][i]
		return [e, d]


class Solution:

	def sumTriangles(self, matrix, n):
		upper_sum = 0
		ans = []
		for i in range(n):
			for j in range(i, n):
				upper_sum += matrix[i][j]
		lower_sum = 0
		for i in range(n):
			for j in range(i + 1):
				lower_sum += matrix[i][j]
		ans.append(upper_sum)
		ans.append(lower_sum)
		return ans


class Solution:

	def sumTriangles(self, matrix, n):
		dsum = 0
		usum = 0
		for i in range(n):
			for j in range(len(matrix[i])):
				if i >= j:
					dsum += matrix[i][j]
				if j >= i:
					usum += matrix[i][j]
		return [usum, dsum]


class Solution:

	def sumTriangles(self, matrix, n):
		upsum = 0
		dsum = 0
		for i in range(n):
			for j in range(n):
				if i >= j:
					dsum += matrix[i][j]
				if i <= j:
					upsum += matrix[i][j]
		return [upsum, dsum]


class Solution:

	def sumTriangles(self, matrix, n):
		i = 0
		sum1 = 0
		sum2 = 0
		while i < n:
			j = i
			while j < n:
				sum1 += matrix[i][j]
				sum2 += matrix[j][i]
				j += 1
			i += 1
		return (sum1, sum2)


class Solution:

	def sumTriangles(self, matrix, n):
		sumUpper = 0
		sumLower = 0
		for i in range(n):
			for j in range(n):
				if j >= i:
					sumUpper += matrix[i][j]
				if i >= j:
					sumLower += matrix[i][j]
		return [sumUpper, sumLower]


class Solution:

	def sumTriangles(self, matrix, n):
		ans = []
		sum_lower = 0
		for i in range(n):
			for j in range(i + 1):
				sum_lower = sum_lower + matrix[i][j]
		sum_upper = 0
		for i in range(n):
			for j in range(i, n):
				sum_upper = sum_upper + matrix[i][j]
		ans.append(sum_upper)
		ans.append(sum_lower)
		return ans


class Solution:

	def sumTriangles(self, matrix, n):
		res = []
		s = 0
		for i in range(n):
			for j in range(i, n):
				s += matrix[i][j]
		res.append(s)
		s = 0
		for i in range(n):
			for j in range(i + 1):
				s += matrix[i][j]
		res.append(s)
		return res


class Solution:

	def sumTriangles(self, l, n):
		up = []
		k = 1
		for i in range(len(l)):
			res = []
			for j in range(i + 1):
				res.append(l[i][j])
			up.append(res)
		lo = []
		for i in range(len(l)):
			res = []
			for j in range(i, len(l)):
				res.append(l[i][j])
			lo.append(res)
		result = []
		su1 = 0
		su2 = 0
		for i in up:
			su1 += sum(i)
		for i in lo:
			su2 += sum(i)
		result.append(su2)
		result.append(su1)
		return result


class Solution:

	def sumTriangles(self, matrix, n):
		d1 = 0
		for i in range(0, n):
			d1 = d1 + matrix[i][i]
		s1 = 0
		s2 = 0
		m1 = 1
		for i in range(0, n):
			if m1 < n:
				for j in range(m1, n):
					s1 = s1 + matrix[i][j]
				m1 = m1 + 1
		m2 = 1
		for i in range(1, n):
			for j in range(0, m2):
				s2 = s2 + matrix[i][j]
			m2 = m2 + 1
		s1 = s1 + d1
		s2 = s2 + d1
		return [s1, s2]


class Solution:

	def sumTriangles(self, matrix, n):
		upper_sum = 0
		lower_sum = 0
		for i in range(0, n):
			for j in range(0, n):
				if i < j:
					upper_sum += matrix[i][j]
				elif i > j:
					lower_sum += matrix[i][j]
				else:
					upper_sum += matrix[i][j]
					lower_sum += matrix[i][j]
		return [upper_sum, lower_sum]


class Solution:

	def sumTriangles(self, matrix, n):
		ut = 0
		lt = 0
		for i in range(0, len(matrix)):
			for j in range(0, len(matrix[0])):
				if j >= i:
					ut += matrix[i][j]
				if j <= i:
					lt += matrix[i][j]
		return [ut, lt]


class Solution:

	def sumTriangles(self, mat, n):
		(ls, up) = (0, 0)
		for x in range(n):
			ls += sum(mat[x][:x + 1])
		for x in range(n):
			up += sum(mat[x][x:])
		return (up, ls)


class Solution:

	def sumTriangles(self, matrix, n):
		ans = []
		su = 0
		sl = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					su += matrix[i][j]
				if i >= j:
					sl += matrix[i][j]
		ans.append(su)
		ans.append(sl)
		return ans


class Solution:

	def sumTriangles(self, matrix, n):
		sum1 = 0
		sum2 = 0
		ans = []
		for i in range(n):
			for j in range(0, i + 1):
				sum1 += matrix[i][j]
		for i in range(n):
			for j in range(i, n):
				sum2 += matrix[i][j]
		ans.append(sum2)
		ans.append(sum1)
		return ans


class Solution:

	def sumTriangles(self, matrix, n):
		su = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					su += matrix[i][j]
		su1 = 0
		for i in range(n):
			for j in range(n):
				if i >= j:
					su1 += matrix[i][j]
		return [su, su1]


class Solution:

	def sumTriangles(self, matrix, n):
		dia = []
		low = []
		up = []
		for i in range(n):
			for j in range(n):
				if i == j:
					dia.append(matrix[i][j])
				elif j > i:
					up.append(matrix[i][j])
				else:
					low.append(matrix[i][j])
		return (sum(dia) + sum(up), sum(dia) + sum(low))


class Solution:

	def sumTriangles(self, matrix, n):
		l = m = 0
		for i in range(n):
			for j in range(i, len(matrix[i])):
				l += matrix[i][j]
			for j in range(0, i + 1):
				m += matrix[i][j]
		return [l, m]


class Solution:

	def sumTriangles(self, matrix, n):
		upper = []
		lower = []
		for i in range(n):
			line = matrix[i][i:]
			line2 = matrix[i][:i + 1]
			upper.extend(line)
			lower.extend(line2)
		a = sum(upper)
		b = sum(lower)
		return [a, b]


class Solution:

	def sumTriangles(self, matrix, n):
		upperT = 0
		lowerT = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					upperT += matrix[i][j]
				if j <= i:
					lowerT += matrix[i][j]
		return [upperT, lowerT]


class Solution:

	def sumTriangles(self, matrix, n):
		l = []
		(s1, s2) = (0, 0)
		for i in range(n):
			for j in range(n):
				if i <= j:
					s1 += matrix[i][j]
		l.append(s1)
		for i in range(n):
			for j in range(n):
				if i >= j:
					s2 += matrix[i][j]
		l.append(s2)
		return l


class Solution:

	def sumTriangles(self, matrix, n):
		upper_sum = 0
		lower_sum = 0
		s = 0
		for i in range(n):
			for j in range(i, n):
				upper_sum += matrix[i][j]
			for k in range(0, i + 1):
				lower_sum += matrix[i][k]
		return [upper_sum, lower_sum]


class Solution:

	def sumTriangles(self, matrix, n):
		upper_tri_sum = 0
		lower_tri_sum = 0
		for row in range(n):
			for col in range(row + 1):
				lower_tri_sum += matrix[row][col]
			for col1 in range(row, n):
				upper_tri_sum += matrix[row][col1]
		return (upper_tri_sum, lower_tri_sum)


class Solution:

	def sumTriangles(self, matrix, n):
		sumu = suml = 0
		for i in range(n):
			for j in range(n):
				if i < j:
					sumu += matrix[i][j]
				elif i > j:
					suml += matrix[i][j]
				else:
					sumu += matrix[i][j]
					suml += matrix[i][j]
		return (sumu, suml)


class Solution:

	def sumTriangles(self, matrix, n):
		u = 0
		l = 0
		t = 0
		for i in range(n):
			for j in range(i, n):
				u += matrix[i][j]
		for k in range(n):
			for p in range(k + 1):
				l += matrix[k][p]
		b = []
		b.append(u)
		b.append(l)
		return b


class Solution:

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


class Solution:

	def sumTriangles(self, matrix, n):
		final = []
		ups = 0
		lps = 0
		for i in range(n):
			for j in range(i + 1):
				ups += matrix[i][j]
		for i in range(n):
			for j in range(i, n):
				lps += matrix[i][j]
		final.append(lps)
		final.append(ups)
		return final


class Solution:

	def sumTriangles(self, matrix, n):
		up = 0
		low = 0
		row = len(matrix)
		col = len(matrix[0])
		for i in range(row):
			for j in range(i, col):
				up = up + matrix[i][j]
		for i in range(row):
			for j in range(i + 1):
				low = low + matrix[i][j]
		return [up, low]


class Solution:

	def sumTriangles(self, matrix, n):
		lsum = 0
		rsuum = 0
		for i in range(n):
			for j in range(n):
				if i < j:
					lsum += matrix[i][j]
				elif j < i:
					rsuum += matrix[i][j]
				elif i == j:
					lsum += matrix[i][j]
					rsuum += matrix[i][j]
		return (lsum, rsuum)


class Solution:

	def sumTriangles(self, matrix, n):
		sum_up = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					sum_up += matrix[i][j]
		sum_down = 0
		for i in range(n):
			for j in range(n):
				if j <= i:
					sum_down += matrix[i][j]
		return (sum_up, sum_down)


class Solution:

	def sumTriangles(self, matrix, n):
		x = y = 0
		for i in range(n):
			for j in range(i, n):
				y += matrix[i][j]
		for i in range(n):
			for j in range(1 + i):
				x += matrix[i][j]
		return [y, x]


class Solution:

	def sumTriangles(self, matrix, n):
		(d1, d2) = (0, 0)
		for i in range(n):
			for j in range(i + 1, n):
				d1 += matrix[i][j]
				d2 += matrix[j][i]
		d = 0
		for i in range(n):
			d += matrix[i][i]
		return [d1 + d, d2 + d]


class Solution:

	def sumTriangles(self, mat, n):
		(i, j) = (0, 0)
		upper_sum = 0
		lower_sum = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					upper_sum += mat[i][j]
		for i in range(n):
			for j in range(n):
				if j <= i:
					lower_sum += mat[i][j]
		return (upper_sum, lower_sum)


class Solution:

	def sumTriangles(self, matrix, n):
		sum = []
		a = 0
		b = 0
		for i in range(n):
			for j in range(i, n):
				a += matrix[i][j]
		sum.append(a)
		for k in range(n):
			for l in range(k + 1):
				b += matrix[k][l]
		sum.append(b)
		return sum


class Solution:

	def sumTriangles(self, matrix, n):
		usum = lsum = 0
		for i in range(n):
			for j in range(i + 1):
				lsum += matrix[i][j]
		for i in range(n - 1, -1, -1):
			for j in range(n - 1, i - 1, -1):
				usum += matrix[i][j]
		return [usum, lsum]


class Solution:

	def sumTriangles(self, arr, n):
		su = []
		ctr = 0
		for x in range(0, n):
			for y in range(x, n):
				ctr += arr[x][y]
		su.append(ctr)
		ctr = 0
		for x in range(0, n):
			for y in range(x, -1, -1):
				ctr += arr[x][y]
		su.append(ctr)
		return su


class Solution:

	def sumTriangles(self, matrix, n):
		n = len(matrix)
		low_tri_sum = 0
		up_tri_sum = 0
		for i in range(n):
			for j in range(n):
				if i >= j:
					low_tri_sum += matrix[i][j]
				if i <= j:
					up_tri_sum += matrix[i][j]
		return [up_tri_sum, low_tri_sum]


class Solution:

	def sumTriangles(self, matrix, n):
		(r, c) = (n, n)
		l = []
		(i, j) = (0, 0)
		upper_sum = 0
		lower_sum = 0
		for i in range(r):
			for j in range(c):
				if i <= j:
					upper_sum += matrix[i][j]
		l.append(upper_sum)
		for i in range(r):
			for j in range(c):
				if j <= i:
					lower_sum += matrix[i][j]
		l.append(lower_sum)
		return l


class Solution:

	def sumTriangles(self, matrix, n):
		u = 0
		l = 0
		for i in range(n):
			for j in range(n):
				if i <= j:
					u += matrix[i][j]
				if i >= j:
					l += matrix[i][j]
		return [u, l]


class Solution:

	def sumTriangles(self, mat, n):
		(r1, r2) = (0, 0)
		for i in range(n):
			for j in range(i, n):
				r1 += mat[i][j]
		for i in range(n):
			for j in range(i + 1):
				r2 += mat[i][j]
		return [r1, r2]


class Solution:

	def sumTriangles(self, matrix, n):
		ut = 0
		k = 0
		for i in range(n):
			for j in range(k, n):
				ut = ut + matrix[i][j]
			k = k + 1
		lt = 0
		for i in range(n):
			for j in range(i + 1):
				lt = lt + matrix[i][j]
		return (ut, lt)


class Solution:

	def sumTriangles(self, matrix, n):
		ans = []
		u = 0
		l = 0
		for i in range(n):
			for j in range(i, n):
				u = u + matrix[i][j]
				l = l + matrix[j][i]
		ans.append(u)
		ans.append(l)
		return ans


class Solution:

	def sumTriangles(self, matrix, n):
		lo = 0
		up = 0
		for i in range(n):
			for j in range(n):
				if j >= i:
					lo += matrix[i][j]
				if j <= i:
					up += matrix[i][j]
		return (lo, up)


class Solution:

	def sumTriangles(self, matrix, n):
		lower = []
		upper = []
		for i in range(n):
			L = matrix[i][:i + 1]
			U = matrix[i][i:]
			for j in L:
				lower.append(j)
			for k in U:
				upper.append(k)
		return [sum(upper), sum(lower)]


class Solution:

	def sumTriangles(self, matrix, n):
		uts = 0
		lts = 0
		for i in range(n):
			for j in range(n):
				if i == j or j > i:
					uts += matrix[i][j]
		for i in range(n):
			for j in range(n):
				if i == j or j < i:
					lts += matrix[i][j]
		return [uts, lts]


class Solution:

	def sumTriangles(self, matrix, n):
		d_u_s = 0
		d_l_s = 0
		for i in range(n):
			d_u_s += sum(matrix[i][i:])
		for i in range(n):
			d_l_s += sum(matrix[i][:i + 1])
		return [d_u_s, d_l_s]


class Solution:

	def sumTriangles(self, matrix, n):
		s1 = 0
		s2 = 0
		s = 0
		l = []
		for i in range(n):
			for j in range(n):
				if i == j:
					s += matrix[i][j]
				elif i < j:
					s1 += matrix[i][j]
				else:
					s2 += matrix[i][j]
		ut = s1 + s
		lt = s2 + s
		l.append(ut)
		l.append(lt)
		return l