File tree 3 files changed +120
-54
lines changed
3 files changed +120
-54
lines changed Original file line number Diff line number Diff line change @@ -73,3 +73,4 @@ int main(){
73
73
return 0 ;
74
74
}
75
75
76
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ Finding the maximum square sub-matrix with all equal elements
3
+
4
+ This is similar to finding the largest square sub matrix with all 1s. The only difference here is
5
+ that the numbers can be any. So basically in this case we maintain another matrix initiazed as zero.
6
+ Then if there is a match between all the four elements, considering the right bottom most element
7
+ to be the reference, we increment the count in the ref matrix at the place as min(i,j) + 1
8
+ everytime.
9
+
10
+ Time and space complexity is same as that of prev matrix question with all 1's
11
+
12
+ O(n^2) each
13
+
14
+ */
15
+
16
+ #include <stdio.h>
17
+ #include <stdlib.h>
18
+ #include <limits.h>
19
+ #include <string.h>
20
+
21
+ int findMin (int a , int b ){
22
+ return a < b ? a : b ;
23
+ }
24
+
25
+ void initRef (int rows , int cols , int ref [rows ][cols ]){
26
+ int i ,j ;
27
+ for (i = 0 ;i < rows ;i ++ ){
28
+ for (j = 0 ;j < cols ;j ++ ){
29
+ ref [i ][j ] = 0 ;
30
+ }
31
+ }
32
+ }
33
+
34
+ void findLargestMatrix (int rows , int cols , int arr [rows ][cols ]){
35
+
36
+ int i , j , max = 0 ;
37
+ int a ,b ,c ;
38
+ int ref [rows ][cols ];
39
+
40
+ initRef (rows , cols , ref );
41
+
42
+ for (i = 1 ;i < rows ;i ++ ){
43
+ for (j = 1 ;j < cols ;j ++ ){
44
+ a = arr [i - 1 ][j - 1 ];
45
+ b = arr [i - 1 ][j ];
46
+ c = arr [i ][j - 1 ];
47
+ if (a == b && b == c && c == arr [i ][j ]){
48
+ ref [i ][j ] = findMin (i ,j ) + 1 ;
49
+ if (max < ref [i ][j ]){
50
+ max = ref [i ][j ];
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ printf ("%d\n" , max );
57
+ }
58
+
59
+ int main (){
60
+
61
+ int arr [4 ][4 ] = {
62
+ {9 ,9 ,9 ,9 },
63
+ {9 ,9 ,9 ,9 },
64
+ {9 ,9 ,9 ,9 },
65
+ {9 ,9 ,9 ,9 }
66
+ };
67
+
68
+ int rows = sizeof (arr )/sizeof (arr [0 ]);
69
+
70
+ int cols = sizeof (arr [0 ])/sizeof (arr [0 ][0 ]);
71
+
72
+ findLargestMatrix (rows , cols , arr );
73
+
74
+ return 0 ;
75
+ }
Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <limits.h>
4
+ #include <string.h>
4
5
5
- void maxProductSubArray (int * arr , int size ){
6
- int i ;
7
- int countNegatives = 0 ;
8
- for (i = 0 ;i < size ;i ++ ){
9
- if (arr [i ] < 0 ){
10
- countNegatives ++ ;
11
- }
12
- }
13
- int lastCountNegativesValues = countNegatives ;
14
-
15
- int curr = 1 , max = INT_MIN ;
16
-
17
- for (i = 0 ;i < size ;i ++ ){
18
- curr = curr * arr [i ];
19
-
20
- if (curr > max ){
21
- max = curr ;
22
- }
23
-
24
- if (arr [i ] < 0 ){
25
- countNegatives -- ;
26
- }
6
+ int findMin (int a , int b ){
7
+ return a < b ? a : b ;
8
+ }
27
9
28
- if ((curr < 0 && countNegatives <= 0 ) || (curr == 0 )){
29
- curr = 1 ;
10
+ void initRef (int rows , int cols , int ref [rows ][cols ]){
11
+ int i ,j ;
12
+ for (i = 0 ;i < rows ;i ++ ){
13
+ for (j = 0 ;j < cols ;j ++ ){
14
+ ref [i ][j ] = 0 ;
30
15
}
31
16
}
32
-
33
- curr = 1 ;
34
- countNegatives = lastCountNegativesValues ;
35
-
36
- for (i = size - 1 ;i >=0 ;i -- ){
37
- curr = curr * arr [i ];
38
- if (curr > max ){
39
- max = curr ;
40
- }
41
-
42
- if (arr [i ] < 0 ){
43
- countNegatives -- ;
44
- }
45
-
46
- if ((curr < 0 && countNegatives <= 0 ) || (curr == 0 )){
47
- curr = 1 ;
17
+ }
18
+
19
+ void findLargestMatrix (int rows , int cols , int arr [rows ][cols ]){
20
+
21
+ int i , j , max = 0 ;
22
+ int a ,b ,c ;
23
+ int ref [rows ][cols ];
24
+
25
+ initRef (rows , cols , ref );
26
+
27
+ for (i = 1 ;i < rows ;i ++ ){
28
+ for (j = 1 ;j < cols ;j ++ ){
29
+ a = arr [i - 1 ][j - 1 ];
30
+ b = arr [i - 1 ][j ];
31
+ c = arr [i ][j - 1 ];
32
+ if (a == b && b == c && c == arr [i ][j ]){
33
+ ref [i ][j ] = findMin (i ,j ) + 1 ;
34
+ if (max < ref [i ][j ]){
35
+ max = ref [i ][j ];
36
+ }
37
+ }
48
38
}
49
39
}
50
40
51
41
printf ("%d\n" , max );
52
- }
42
+ }
53
43
54
44
int main (){
55
- int cases ;
56
- scanf ("%d" ,& cases );
57
- int i ;
58
- for (i = 0 ;i < cases ;i ++ ){
59
- int size ;
60
- scanf ("%d" ,& size );
61
- int j ;
62
- int arr [size ];
63
- for (j = 0 ;j < size ;j ++ ){
64
- scanf ("%d" ,& arr [j ]);
65
- }
66
- maxProductSubArray (arr , size );
45
+
46
+ int arr [4 ][4 ] = {
47
+ {9 ,9 ,9 ,9 },
48
+ {9 ,9 ,9 ,9 },
49
+ {9 ,9 ,9 ,9 },
50
+ {9 ,9 ,9 ,9 }
51
+ };
52
+
53
+ int rows = sizeof (arr )/sizeof (arr [0 ]);
54
+
55
+ int cols = sizeof (arr [0 ])/sizeof (arr [0 ][0 ]);
56
+
57
+ findLargestMatrix (rows , cols , arr );
67
58
68
- }
69
59
return 0 ;
70
60
}
You can’t perform that action at this time.
0 commit comments