-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maimumordersquaresubmatrix.java
89 lines (74 loc) · 1.66 KB
/
Maimumordersquaresubmatrix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package geeks_prac;
import java.util.*;
// The program computes the maximum size square sub-matrix with all 1s
public class Maimumordersquaresubmatrix {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Entering the order of matrix");
int m;
int n;
System.out.println("The row order is");
m = scan.nextInt();
System.out.println("The column order is");
n = scan.nextInt();
System.out.println("Enter the matrix :");
int i,j;
int arr[][] = new int[10][10];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[i][j] = scan.nextInt();
}
}
System.out.println("Printing the matrix input");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.println(arr[i][j]);
}
System.out.println("\n");
}
int sub[][] = new int[m][n];
for(i=0;i<m;i++)
{
j=0;
sub[i][j] = arr[i][j];
}
for(j=0;j<n;j++)
{
i=0;
sub[i][j] = arr[i][j];
}
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
{
if(arr[i][j] == 0)
{
sub[i][j] = 0;
}
else if(arr[i][j] == 1)
{
sub[i][j] = Math.min(arr[i-1][j],Math.min(arr[i][j-1],arr[i-1][j-1]))+1;
}
}
}
//Final output
int maxorder = 0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(sub[i][j] > maxorder)
{
maxorder = sub[i][j];
}
}
}
System.out.println("The maximum order of the array with square of 1's are");
System.out.println(maxorder);
} // End of main
}