-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.java
More file actions
65 lines (60 loc) · 1.86 KB
/
Q5.java
File metadata and controls
65 lines (60 loc) · 1.86 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//.Problem statement
// For a given two-dimensional square matrix of size (N x N). Find the total sum of elements on both the diagonals and at all the four boundaries.
//
// Sample input 1:
// 1
// 3
// 1 2 3
// 4 5 6
// 7 8 9
// Sample Output 1:
// 45
// Explanation for Sample Output 1:
// 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.
package DAY_3;
import java.util.Scanner;
public class Q5
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the rows and columns");
int n=sc.nextInt();
int arr[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
// if(i==j)
// {
// sum=sum+arr[i][j];
// }
// else if(i+j==2)
// {
// sum=sum+arr[i][j];
// }
if(i==j||(i+j+1==n-1)||i==0||j==0||i==n-1||j==n-1)
{
sum=sum+arr[i][j];
}
}
}
System.out.println(sum);
}
}