-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion35.c
75 lines (57 loc) · 1.53 KB
/
question35.c
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
/*
Finding the maximum square sub-matrix with all equal elements
This is similar to finding the largest square sub matrix with all 1s. The only difference here is
that the numbers can be any. So basically in this case we maintain another matrix initiazed as zero.
Then if there is a match between all the four elements, considering the right bottom most element
to be the reference, we increment the count in the ref matrix at the place as min(i,j) + 1
everytime.
Time and space complexity is same as that of prev matrix question with all 1's
O(n^2) each
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int findMin(int a, int b, int c){
return (a<b)?((a<c)?a:c):((b<c)?b:c);
}
void initRef(int rows, int cols, int ref[rows][cols]){
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
ref[i][j] = 1;
}
}
}
void findLargestMatrix(int rows, int cols, int arr[rows][cols]){
int i, j, max = 0;
int a,b,c;
int ref[rows][cols];
initRef(rows, cols, ref);
for(i=1;i<rows;i++){
for(j=1;j<cols;j++){
a = arr[i-1][j-1];
b = arr[i-1][j];
c = arr[i][j-1];
if(a == arr[i][j] && b == arr[i][j] && c == arr[i][j]){
ref[i][j] = findMin(ref[i-1][j-1],ref[i][j-1],ref[j-1][i]) + 1;
if(max < ref[i][j]){
max = ref[i][j];
}
}
}
}
printf("%d\n", max);
}
int main(){
int arr[4][4] = {
{0,9,9,0},
{0,9,9,9},
{0,9,9,9},
{0,9,9,9}
};
int rows = sizeof(arr)/sizeof(arr[0]);
int cols = sizeof(arr[0])/sizeof(arr[0][0]);
findLargestMatrix(rows, cols, arr);
return 0;
}