forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
26.12.2023.cpp
91 lines (77 loc) · 2.13 KB
/
26.12.2023.cpp
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
90
91
class Solution{
public:
vector<int> findZero(vector<int>&temp, int n){
int start=-1, end=-1, maxLength=0;
unordered_map<int,int>mp;
int sum=0;
for(int i=0;i<n;i++){
sum+=temp[i];
if(temp[i]==0 && maxLength==0){
maxLength=1;
start=i;
end=i;
}
if(sum==0){
maxLength=i+1;
start=0;
end=i;
}
if(mp.find(sum) !=mp.end()){
if(maxLength < i-(mp[sum])){
maxLength=i-(mp[sum]);
start=mp[sum]+1;
end=i;
}
}
else{
mp[sum]=i;
}
}
if(maxLength==0){
return {0, -1, -1};
}
else{
return {1, start, end};
}
}
vector<vector<int>> sumZeroMatrix(vector<vector<int>> a){
//Code Here
int row=a.size();
int col=a[0].size();
int f_up=-1, f_down=-1, f_right=-1, f_left=-1;
int f_ele=0;
for(int left=0;left<col;left++){
vector<int>temp(row,0);
for(int right=left;right<col;right++){
for(int i=0;i<row;i++){
temp[i]+=a[i][right];
}
vector<int>result=findZero(temp, row);
if(result[0]==1){
int up=result[1];
int down=result[2];
int ele= (down-up+1)*(right-left+1);
if(f_ele < ele){
f_ele = ele;
f_down=down;
f_left=left;
f_right=right;
f_up=up;
}
}
}
}
vector<vector<int>>ans;
if(f_ele==0){
return ans;
}
for(int i=f_up;i<=f_down;i++){
vector<int>column;
for(int j=f_left;j<=f_right;j++){
column.push_back(a[i][j]);
}
ans.push_back(column);
}
return ans;
}
};