Total Sum on the Boundaries and Diagonals
Send Feedback
Code Genius is an all-in-one platform for bloggers, YouTubers, job seekers, and tech enthusiasts. It offers resources on content creation, job search, cooling pads, latest tech info, smartphones, headphones, laptops, electronics, and online earning.
Total Sum on the Boundaries and Diagonals
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First line of each test case or query contains a single integer value, 'N' representing the 'rows' and 'columns' for the two-dimensional square matrix.
Second line onwards, the next 'N' lines or rows represent the ith row values.
Each of the ith row constitutes 'N' column values separated by a single space.
For each test case, print the single integer denoting the sum.
Output for every test case will be printed in a seperate line.
1 <= t <= 10^2
0 <= N <= 10^3
Time Limit: 1sec
1
3
1 2 3
4 5 6
7 8 9
45
The boundary elements are 1, 2, 3, 6, 9, 8, 7 and 4.
The first-diagonal elements are 1, 5 and 9.
The second-diagonal elements are 3, 5 and 7.
We just need to add all these numbers making sure that no number is added twice. For example, '1' is both a boundary element and a first-diagonal element similarly, '5' contributes to both the diagonals but they won't be added twice.
Hence, we add up, [1 + 2 + 3 + 6 + 9 + 8 + 7 + 4 + 5] to give 45 as the output.
2
5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
4
1 2 3 10
4 5 6 11
7 8 9 12
13 14 15 16
273
136
code for this
public class Solution {
public static void totalSum(int[][] mat) { int n = mat.length; int sum = 0;
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1 || i == j || j == n - i - 1) { sum = sum+mat[i] [j]; } } }
System.out.println(sum); }}
Comments
Post a Comment