Array ( [0] => [1] => questions [2] => Strings [3] => Diagonal-Sum )
Given a 2D array of elements. You have to find the diagonal sum of elements in given array. Note: Before applying check whether it’s possible or not.
First line contains two integers M and N. Next M lines contains N integers separated by spaces.
Print the diagonal sum value.
3 3 1 2 3 4 5 6 7 8 9
15
2 2 1 2 3 4
5
3 3 3 2 1 6 5 4 9 8 7
15
Login to see Discussion
Approach 1: Brute Force
Step-i) Initialize sum to 0
Step-ii) run loop from i=0 to i
Step-v) return the sum
Time Complexity: O(n^2)
Space Complexity: O(1)
Approach 2: Using Liner Approach
Step-i) Initialize sum to 0
Step-ii) run loop from i= 0 to i
Step-iv) return the sum
Time Complexity: O(n)
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution