forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max_Sum_Rectangle.c
89 lines (81 loc) · 2.12 KB
/
Max_Sum_Rectangle.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//C code to implement maximum sum rectangle in a 2D matrix
#include <stdio.h>
#include<stdbool.h>
#include<string.h>
#include <limits.h>
int main() {
// R & C are the number of rows and columns
int R, C;
printf("Input-->\n");
printf("Enter the row-sze: \n");
scanf("%d", & R);
printf("Enter the column-sze: \n");
scanf("%d", & C);
int matrix[R][C], temp[R], top, bottom;
printf("Enter the elements of 2-D matrix :\n");
for (int ol = 0; ol < R; ol++) {
for (int inloop= 0; inloop < C; inloop++) {
scanf("%d", & matrix[ol][inloop]);
}
}
int maxSum = INT_MIN, finalTop, finalBottom, finalRight, finalLeft;
for (int Left= 0; Left < C; Left++) {
// Initialize the all elements of temp as 0
memset(temp, 0, sizeof(temp));
for (int Right = Left; Right < C; Right++) {
for (int i = 0; i < R; i++) temp[i] += matrix[i][Right];
int max = INT_MIN, S = 0, localTop = 0, sum;
bool flag = false;
for (int index = 0; index < R; index++) {
if (temp[index] >= 0) flag = true;
S = S + temp[index];
if (S < 0) {
S = 0;
localTop = index+ 1;
} else if (S > max) {
max = S;
top = localTop;
bottom = index;
}
}
if (flag) {
sum = max;
} else {
max = INT_MIN;
//To find the maximum element in array
for (int index = 0; index < R; index++) {
if (max < temp[index]) {
max = temp[index];
top = bottom = index;
}
}
sum = max;;
}
// Comparison of sum with maximum sum
if (sum > maxSum) {
maxSum = sum;
finalTop = top;
finalBottom = bottom;
finalLeft = Left;
finalRight = Right;
}
}
}
printf("\nOutput-->\n");
printf("Maximum sumRectangle is : %d\n", maxSum);
return 0;
}
/*
Input-->
Enter the row-sze: 4
Enter the column-sze: 5
Enter the elements of 2-D matrix :
1 2 -1 -4 -20
-8 -3 4 2 1
3 8 10 1 3
-4 -1 1 7 -6
Output-->
Maximum sumRectangle is : 29
Time Complexity: O(cols^2 * rows)
Space Complexity: O(rows)
*/