-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxsubexp.cpp
More file actions
79 lines (64 loc) · 1.32 KB
/
maxsubexp.cpp
File metadata and controls
79 lines (64 loc) · 1.32 KB
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
#include<iostream>
using namespace std;
class max1
{
public:
int left,right,sum;
max1(){
sum=-2222;
}
};
class sub
{
public:
int n,*A;
sub(){
cout<<"enter the no of element :";
cin>>n;
A=new int[n];
cout<<"enter the no of ellement :";
for(int i=0;i<n;i++)
{
cout<<"A["<<i<<"]=";
cin>>A[i];
}
}
max1* max_cross_sum(int *A,int low,int mid ,int high);
max1* max_sub_arr(int *A,int low,int high);
};
max1* sub::max_cross_sum(int *A,int low,int mid ,int high)
{
max1 *p;
int left_sum=-2222;
int summ=0,max_left,max_right;
for(int i=mid;i>=0;i--)
{
summ=summ+A[i];
if(summ>left_sum)
{
left_sum=summ;
max_left=i;
}
}
int right_sum=-3333;
summ=0;
for(int j=mid+1;j<high;j++)
{
summ=summ+A[j];
if(summ>right_sum)
{
right_sum=summ;
max_right=j;
}
}
p->left=max_left;
p->right=max_right;
p->sum=left_sum+right_sum;
return p;
}
int main()
{
sub s;
max1 *k=s.max_cross_sum(s.A,0,4,8);
cout<<k->left<<" "<<k->right<<" "<<k->sum;
}