Diagonal sum | Practice | GeeksforGeeks

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
0
cshubham4393 months ago
 int sum = 0;       for(int i =0; i<matrix.length; i++){           for(int j = 0; j<matrix.length; j++){               if(i==j){                   sum = sum+matrix[i][j];               }               if(i+j==matrix.length-1){                   sum = sum+matrix[i][j];               }           }       }       return sum;
-2
keshrishivam41143 months ago
complexity - O(n)
def DiagonalSum(self, matrix):    summ=0 for i in range(len(matrix)):     summ += matrix[i][i]+matrix[i][len(matrix)-1-i] return summ
0
pankajkumarravi6 months ago
public int DiagonalSum(int[][] matrix){int sd1 = 0 , sd2 = 0;int j = 0 , len = matrix.length - 1 , j1 = 0;for(int i = 0; i < matrix.length; i++){j = i;sd1 = sd1 + matrix[i][j];j1 = len - j;sd2 = sd2 + matrix[i][j1];}return sd1 + sd2;}
-1
pankajkumarravi6 months ago
Code looks working , here in this platform, few cases failed
 
    public int DiagonalSum(int[][] matrix) {       // code here       int diagonalSum=0;       for ( int i=0;i<matrix.length;i++){           for (int j=0;j<matrix.length;j++){               if (i==j)                   diagonalSum =diagonalSum+matrix[i][j];           }       }       return diagonalSum;   }
0
mrshobhit7 months ago
Python 3:
Execution Time - 0.1 Sec
 
 
import numpy as np
class Solution:
	def DiagonalSum(self, matrix):
		# Code here
		matrix=np.array(matrix)
		return (matrix.trace())+(matrix[::-1].trace())
0
Hardik Gupta8 months ago
Hardik Gupta
SOLUTION IN JAVA
class Solution{    public int DiagonalSum(int[][] matrix)    {        int sd1 = 0 , sd2 = 0;        int j = 0 , len = matrix.length - 1 , j1 = 0;        for(int i = 0; i < matrix.length; i++){            j = i;            sd1 = sd1 + matrix[i][j];            j1 = len - j;            sd2 = sd2 + matrix[i][j1];        }        return sd1 + sd2;    }}
0
Shreya Nayak9 months ago
Shreya Nayak
int DiagonalSum(vector<vector<int> >& matrix) {        // Code here        int s=0,s1=0;        for(int i=0;i<matrix.size();i++) {="" for(int="" j="0;j&lt;matrix.size();j++)" {="" if(i="=j)" s+="matrix[i][j];" if(i="=matrix.size()-j-1)" s1+="matrix[i][j];" }="" }="" return="" (s+s1);="" }="">
0
Shreyansh Kumar Singh1 year ago
Shreyansh Kumar Singh
https://uploads.disquscdn.c...
0
Shreyansh Kumar Singh
This comment was deleted.
0
Tulsi Dey2 years ago
Tulsi Dey
Solution in Java:Execution time: 0.15 https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.