-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.java
51 lines (46 loc) · 1.24 KB
/
7.java
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
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class GFG
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
int matrix[][] = new int[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
Solution ob = new Solution();
ArrayList<Integer> ans = ob.sumTriangles(matrix,n);
for (Integer val: ans)
System.out.print(val+" ");
System.out.println();
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
//Function to return sum of upper and lower triangles of a matrix.
static ArrayList<Integer> sumTriangles(int mat[][], int n)
{
// code here
ArrayList<Integer> al = new ArrayList<>();
int a = 0;
int b = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j<=i) a+=mat[i][j];
if(j>=i) b+=mat[i][j];
}
}
al.add(b);
al.add(a);
return al;
}
}