forked from shikharkohli/c---files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxsum.cpp
More file actions
executable file
·43 lines (38 loc) · 705 Bytes
/
Copy pathmaxsum.cpp
File metadata and controls
executable file
·43 lines (38 loc) · 705 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
#include<iostream>
#define MAX 1000
using namespace std;
int maxsum = 0;
void max_sum(int arr[][MAX],int i,int j,int sum,int n)
{
if(i > n - 1)
return;
int newsum = sum + arr[i][j];
if(newsum>sum)
{
sum += arr[i][j];
//cout<<"SUM"<<sum;
if(sum > maxsum)
maxsum = sum;
}
//cout<<"("<<i<<","<<j<<")-->";
max_sum(arr,i+1,j,sum,n);
if(j <= i)
max_sum(arr,i+1,j+1,sum,n);
}
int main()
{
int n,arr[MAX][MAX];
cin>>n;
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++)
arr[i][j] = -1;
for(int i=0;i<n;i++)
for(int j=0;j <= i; j++)
//cin>>arr[i][j];
arr[i][j] = j + 1;
int sum = 0;
maxsum = sum;
max_sum(arr,0,0,sum,n);
cout<<"\nMax sum is "<<maxsum<<endl;
return 0;
}