-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxTraceOfSubMatrix.java
More file actions
56 lines (51 loc) · 1.3 KB
/
MaxTraceOfSubMatrix.java
File metadata and controls
56 lines (51 loc) · 1.3 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
import java.util.Scanner;
public class MaxTraceOfSubMatrix
{
static int n;
public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
int t=scan.nextInt();
for(int t_ =0; t_<t ; t_++)
{
int trace=0,temp;
n=scan.nextInt();
int[][] a=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n ; j++)
{
a[i][j]=scan.nextInt();
if(i==j)
trace=trace+a[i][i];
}
}
for(int i=0;i<n-1;i++)
{
temp=maxTrace(a,i);
if(temp>trace)
trace=temp;
}
System.out.println(trace);
}
}
public static int maxTrace(int[][] a,int k)
{
int trace =Integer.MIN_VALUE,temp;
for(int i=0 ; i<n-k ;i++)
{
for(int j=0;j<n-k ;j++)
{
temp=0;
//we are at submatrix (i,j)as 1st element
for(int l=0 ;l<=k ;l++)
{
temp=temp+a[i+l][j+l];
}
if(temp>trace)
trace=temp;
}
}
return trace;
}
}