-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.cpp
59 lines (59 loc) · 1.15 KB
/
code.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
#include<bits/stdc++.h>
#define inf 100
using namespace std;
int maxup=0,maxdown=0,maxleft,maxright;
int Kadane(int a[],int n)
{
int max_current,max_global;
max_current=max_global=a[0];
for(int i=1;i<n;i++)
{
if(a[i]>max_current+a[i])
maxup=i;
else if(max_global<max_current+a[i])
maxdown=i;
max_current=max(a[i],max_current+a[i]);
if(max_current>max_global)
max_global=max_current;
}
return max_global;
}
int main()
{
int n,a[inf][inf],b[inf],i,max=0,max_up,max_down,max_left,max_right,j,left,right,temp;
cout<<"Enter n: ";
cin>>n;
cout<<"Enter "<<n*n<<" integers : ";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(left=0;left<n;left++)
{
for(i=0;i<n;i++)
b[i]=0;
for(right=left;right<n;right++)
{
for(j=0;j<n;j++)
b[j]=b[j]+a[j][right];
temp=Kadane(b,n);
if(max<temp)
{
max=temp;
max_left=left;
max_right=right;
max_up=maxup;
max_down=maxdown;
}
}
}
cout<<"Maximum subarray sum = "<<max<<endl;
//cout<<max_up<<endl<<max_down<<endl<<max_left<<endl<<max_right;
for(i=max_up;i<=max_down;i++)
{
for(left=max_left;left<=max_right;left++)
{
cout<<a[i][left]<<" ";
}
cout<<endl;
}
}