Given a matrix Grid[][] of size NxN. Calculate the absolute difference between the sums of its diagonals.
Example 1:
Input:
N=3
Grid=[[1,2,3],[4,5,6],[7,8,9]]
Output: 
0
Explanation:
Sum of primary diagonal = 1+5+9 = 15.
Sum of secondary diagonal = 3+5+7 = 15.
Difference = |15 - 15| = 0.
Example 2:
Input:
N=3
Grid=[[1,1,1],[1,1,1],[1,1,1]]
Output:
0
Explanation:
Sum of primary diagonal = 1+1+1=3.
Sum of secondary diagonal = 1+1+1=3.
Difference = |3-3| = 0.
Your Task:
You don't need to read input or print anything.Your task is to complete the function diagonalSumDifference() which takes an integer N and a 2D array Grid as input parameters and returns the absolutes difference between the sums of its diagonals.
Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=1000
-1000<=Grid[i][j]<=1000, for 0<=i,j

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3

class Solution:
    def diagonalSumDifference(self,N,Grid):
        #code here

Solutions:
class Solution:

	def diagonalSumDifference(self, N, Grid):
		(Grid1, Grid2) = (0, 0)
		for i in range(N):
			for j in range(N):
				if i == j:
					Grid1 += Grid[i][j]
				if i + j == N - 1:
					Grid2 += Grid[i][j]
		return abs(Grid1 - Grid2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sum1 = 0
		sum2 = 0
		for i in range(0, N):
			sum1 += Grid[i][i]
			sum2 += Grid[i][N - 1 - i]
		abs_diff = abs(sum1 - sum2)
		return abs_diff


class Solution:

	def diagonalSumDifference(self, N, Grid):
		arr = Grid
		(s1, s2) = (0, 0)
		for i in range(len(arr)):
			for j in range(len(arr[i])):
				if i == j:
					s1 += arr[i][j]
		for i in range(len(arr)):
			arr[i].reverse()
		for i in range(len(arr)):
			for j in range(len(arr[i])):
				if i == j:
					s2 += arr[i][j]
		return abs(s1 - s2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		n = len(Grid)
		sumL = 0
		sumR = 0
		for i in range(n):
			for j in range(n):
				if i == j:
					sumL += Grid[i][j]
				if i + j == n - 1:
					sumR += Grid[i][j]
		return abs(sumL - sumR)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sum = 0
		sum1 = 0
		for i in range(len(Grid)):
			for j in range(len(Grid)):
				if i == j:
					sum += Grid[i][j]
				if i + j == len(Grid) - 1:
					sum1 += Grid[i][j]
		return abs(sum - sum1)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		d1 = 0
		d2 = 0
		for i in range(0, N):
			d1 += Grid[i][i]
			d2 += Grid[i][N - i - 1]
		return abs(d1 - d2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		s = 0
		d = 0
		for i in range(len(Grid)):
			s += Grid[i][i]
			d += Grid[i][len(Grid) - 1 - i]
		return abs(s - d)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		t1 = 0
		t2 = 0
		for i in range(N):
			t1 = t1 + Grid[i][i]
		for i in range(N):
			t2 = t2 + Grid[i][N - i - 1]
		return abs(t1 - t2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(s1, s2) = (0, 0)
		for i in range(N):
			s1 = s1 + Grid[i][i]
		for i in range(N):
			s2 = s2 + Grid[i][N - i - 1]
		return abs(s1 - s2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		a = 0
		b = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					a += Grid[i][j]
				if j == N - i - 1:
					b += Grid[i][j]
		return max(a, b) - min(a, b)


class Solution:

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


class Solution:

	def diagonalSumDifference(self, N, Grid):
		result = 0
		for i in range(N):
			result += Grid[i][i]
			result -= Grid[i][N - 1 - i]
		return abs(result)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sum1 = 0
		sum2 = 0
		for i in range(N):
			sum1 += Grid[i][i]
			sum2 += Grid[i][N - (1 + i)]
		a = abs(sum1 - sum2)
		if a == 0:
			return 0
		else:
			return a


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sum1 = 0
		sum2 = 0
		for i in range(0, N):
			for j in range(0, N):
				if i == j:
					sum1 = sum1 + Grid[i][j]
		for i in range(0, N):
			for j in range(0, N):
				if i + j == N - 1:
					sum2 = sum2 + Grid[i][j]
		diff = sum1 - sum2
		if diff >= 0:
			return diff
		else:
			new = -1 * diff
			return new


class Solution:

	def diagonalSumDifference(self, N, Grid):
		direct_sum = 0
		secondary_sum = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					direct_sum = direct_sum + Grid[i][j]
				if j == N - i - 1:
					secondary_sum = secondary_sum + Grid[i][j]
		return abs(direct_sum - secondary_sum)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		left = 0
		right = 0
		for i in range(len(Grid)):
			for j in range(len(Grid[0])):
				if i == j:
					left += Grid[i][j]
				if i + j == len(Grid) - 1:
					right += Grid[i][j]
		return abs(right - left)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(mainD, offD) = (0, 0)
		for i in range(N):
			for j in range(N):
				if i == j:
					mainD += Grid[i][j]
				if i + j == N - 1:
					offD += Grid[i][j]
		return abs(mainD - offD)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		primary = 0
		secondary = 0
		for i in range(N):
			for j in range(N):
				if i - j == 0:
					primary += Grid[i][j]
				elif i + j == N - 1:
					secondary += Grid[i][j]
		if N % 2 != 0:
			secondary += Grid[N // 2][N // 2]
		diff = abs(primary - secondary)
		return diff


class Solution:

	def diagonalSumDifference(self, N, Grid):
		diag = [0 for _ in range(2)]
		for i in range(N):
			diag[0] += Grid[i][i]
			diag[1] -= Grid[i][N - 1 - i]
		return abs(sum(diag))


class Solution:

	def diagonalSumDifference(self, N, mat):
		s1 = 0
		for i in range(N):
			s1 += mat[i][i]
		s2 = 0
		j = 0
		for i in range(N - 1, -1, -1):
			s2 += mat[j][i]
			j += 1
		return abs(s2 - s1)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		length = len(Grid)
		(ps, ds) = (0, 0)
		for i in range(N):
			ps += Grid[i][i]
			ds += Grid[i][N - i - 1]
		return abs(ps - ds)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		r = []
		l = []
		for i in range(N):
			for j in range(N):
				if i == j:
					r.append(Grid[i][j])
		i = 0
		j = 0
		while i < N and j < N:
			l.append(Grid[i][N - j - 1])
			i += 1
			j += 1
		return abs(sum(r) - sum(l))


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sumi1 = 0
		sumi2 = 0
		for i in range(N):
			sumi1 += Grid[i][i]
			sumi2 += Grid[i][N - i - 1]
		return abs(sumi1 - sumi2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		self.N = N
		self.Grid = Grid
		suml = 0
		sumr = 0
		diff = 0
		for i in range(self.N):
			suml = suml + self.Grid[i][i]
			for j in range(self.N):
				if i + j == self.N - 1:
					sumr = sumr + self.Grid[i][j]
		return abs(suml - sumr)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		S1 = S2 = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					S1 += Grid[i][j]
		for i in range(N):
			for j in range(N):
				if i + j == N - 1:
					S2 += Grid[i][j]
		return abs(S1 - S2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		p = 0
		s = 0
		for i in range(N):
			p += Grid[i][i]
			s += Grid[i][N - 1 - i]
		if p - s < 0:
			return s - p
		return p - s


class Solution:

	def diagonalSumDifference(self, N, Grid):
		x = 0
		y = 0
		for i in range(N):
			x += Grid[i][i]
			y += Grid[i][N - 1 - i]
		return abs(x - y)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		pridia = 0
		secdia = 0
		for i in range(N):
			pridia += Grid[i][i]
			secdia += Grid[i][N - i - 1]
		return abs(pridia - secdia)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(sum_l, sum_r) = (0, 0)
		for i in range(N):
			sum_l += Grid[i][i]
			sum_r += Grid[i][N - 1 - i]
		return abs(sum_l - sum_r)


import math

class Solution:

	def diagonalSumDifference(self, N, a):
		spd = 0
		ssd = 0
		j = 0
		for i in range(0, N):
			spd += a[j][i]
			ssd += a[j][N - i - 1]
			j += 1
		return abs(spd - ssd)


class Solution:

	def diagonalSumDifference(self, N, matrix):
		sum1 = 0
		sum2 = 0
		for i in range(len(matrix)):
			for j in range(len(matrix)):
				if i == j:
					sum1 = sum1 + matrix[i][j]
				if i + j == len(matrix) - 1:
					sum2 = sum2 + matrix[i][j]
		if sum1 > sum2:
			return sum1 - sum2
		else:
			return sum2 - sum1


class Solution:

	def diagonalSumDifference(self, N, Grid):
		primary_dig = 0
		secondary_dig = 0
		for i in range(N):
			primary_dig += Grid[i][i]
			secondary_dig += Grid[i][N - 1 - i]
		diff = primary_dig - secondary_dig
		return abs(diff)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		import numpy as np
		matrix = np.array(Grid)
		return abs(matrix.trace() - matrix[::-1].trace())


class Solution:

	def diagonalSumDifference(self, N, Grid):
		l = []
		m = []
		k = N - 1
		for i in range(N):
			l.append(Grid[i][i])
			m.append(Grid[i][k])
			k -= 1
		return abs(sum(l) - sum(m))


class Solution:

	def diagonalSumDifference(self, N, Grid):
		primary_diagonal = 0
		secondary_diagonal = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					primary_diagonal += Grid[i][j]
				if i + j == N - 1:
					secondary_diagonal += Grid[i][j]
		return abs(primary_diagonal - secondary_diagonal)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		p_diag = 0
		diag = 0
		for i in range(len(Grid)):
			for j in range(len(Grid[i])):
				if i == j:
					p_diag += Grid[i][j]
		for i in Grid:
			i.reverse()
		for i in range(len(Grid)):
			for j in range(len(Grid[i])):
				if i == j:
					diag += Grid[i][j]
		sum = p_diag - diag
		if sum < 0:
			return -1 * sum
		return sum


class Solution:

	def diagonalSumDifference(self, N, Grid):
		diag1 = diag2 = 0
		for i in range(N):
			diag1 += Grid[i][i]
			diag2 += Grid[i][N - i - 1]
		return abs(diag1 - diag2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		prmry_sum = 0
		secd_sum = 0
		for i in range(0, N):
			for j in range(N - 1 - (N - 1 - i), N, N):
				prmry_sum += Grid[i][j]
				secd_sum += Grid[i][N - 1 - j]
		value = prmry_sum - secd_sum
		if value < 0:
			return 0 - value
		else:
			return value


class Solution:

	def diagonalSumDifference(self, N, Grid):
		pr = 0
		se = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					pr += Grid[i][j]
				if i + j == N - 1:
					se += Grid[i][j]
		return max(pr, se) - min(pr, se)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		pr = 0
		sec = 0
		for i in range(N):
			pr += Grid[i][i]
			sec += Grid[i][N - i - 1]
		return abs(pr - sec)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		count1 = 0
		count2 = 0
		for i in range(N):
			count1 += Grid[i][i]
			count2 += Grid[i][-1 - i]
		return abs(count1 - count2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(x, y) = (0, 0)
		for i in range(N):
			for j in range(N):
				if i == j:
					x += Grid[i][j]
		(i, j) = (0, N - 1)
		while i < N and j >= 0:
			y += Grid[i][j]
			i += 1
			j -= 1
		return abs(x - y)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		spd = 0
		ssd = 0
		for i in range(N):
			spd += Grid[i][i]
			ssd += Grid[i][N - i - 1]
		return abs(spd - ssd)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		k1 = k2 = 0
		for i in range(N):
			k1 += Grid[i][i]
			k2 += Grid[i][N - i - 1]
		return abs(k1 - k2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		a = 0
		b = 0
		for i in range(0, N):
			for j in range(0, N):
				if i == j:
					a += Grid[i][j]
				if i == N - j - 1:
					b += Grid[i][j]
		c = abs(b - a)
		return c


class Solution:

	def diagonalSumDifference(self, N, Grid):
		import numpy as np
		d1 = sum(np.diag(Grid))
		d2 = sum(np.diag(Grid[::-1]))
		return abs(d1 - d2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sum1 = 0
		sum2 = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					sum1 += Grid[i][j]
		i = 0
		j = N - 1
		while i <= N - 1 and j >= 0:
			sum2 += Grid[i][j]
			i += 1
			j = j - 1
		return abs(sum1 - sum2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(psum, ssum) = (0, 0)
		for i in range(N):
			psum += Grid[i][i]
			ssum += Grid[i][N - i - 1]
		return abs(psum - ssum)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		prime = []
		second = []
		for i in range(N):
			for j in range(N):
				if i == j:
					prime.append(Grid[i][j])
				if i + j == N - 1:
					second.append(Grid[i][j])
		return abs(sum(prime) - sum(second))


class Solution:

	def diagonalSumDifference(self, N, Grid):
		(primary, secondary) = (0, 0)
		for i in range(N):
			primary += Grid[i][i]
		i = 0
		for j in range(N - 1, -1, -1):
			secondary += Grid[i][j]
			i += 1
		return abs(primary - secondary)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		count = 0
		diagonal1 = 0
		diagonal2 = 0
		for x in Grid:
			diagonal1 += x[count]
			count += 1
		count = N - 1
		for x in Grid:
			diagonal2 += x[count]
			count -= 1
		return abs(diagonal1 - diagonal2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		primary_counter = 0
		primary = 0
		secondary_counter = N - 1
		secondary = 0
		for i in Grid:
			primary += i[primary_counter]
			primary_counter += 1
			secondary += i[secondary_counter]
			secondary_counter -= 1
		return abs(primary - secondary)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		dig1 = 0
		dig2 = 0
		rev_Grid = Grid[::-1]
		for i in range(N):
			dig1 += Grid[i][i]
			dig2 += rev_Grid[i][i]
		return abs(dig1 - dig2)


class Solution:

	def diagonalSumDifference(self, N, Grid):
		a = 0
		b = 0
		for i in range(N):
			a += Grid[i][i]
		for i in range(N):
			b += Grid[i][N - 1 - i]
		ans = abs(a - b)
		return ans


class Solution:

	def diagonalSumDifference(self, N, Grid):
		sm_primary = 0
		sm_alternate = 0
		for i in range(N):
			for j in range(N):
				if i == j:
					sm_primary += Grid[i][j]
				elif i + j + 1 == N:
					sm_alternate += Grid[i][j]
		if N & 1:
			sm_alternate += Grid[N // 2][N // 2]
		return abs(sm_alternate - sm_primary)