-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximum sum Rectangle.cpp
More file actions
45 lines (38 loc) · 863 Bytes
/
Copy pathMaximum sum Rectangle.cpp
File metadata and controls
45 lines (38 loc) · 863 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int maxSum(int, int);
int kadanes(int[], int);
int main(){
int t, n, m;
cin >> t;
while(t--){
cin >> n >> m;
cout << maxSum(n, m) << endl;
}
return 0;
}
int maxSum(int n, int m){
int mat[n][m], res = INT_MIN;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> mat[i][j];
for(int l = 0; l < m; l++){
int arr[n] = {0};
for(int r = l; r < m; r++){
for(int i = 0; i < n; i++)
arr[i] += mat[i][r];
res = max(res, kadanes(arr, n));
}
}
return res;
}
int kadanes(int arr[], int n){
int res = INT_MIN, curr = 0;
for(int i = 0; i < n; i++){
curr += arr[i];
res = max(res, curr);
if(curr < 0)
curr = 0;
}
return res;
}