-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmaximum_subarray_sum.cpp
100 lines (78 loc) · 1.9 KB
/
maximum_subarray_sum.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
92
93
94
95
96
97
98
99
100
/*
Maximum subarray sum possible
*/
#include<iostream>
#include<climits>
using namespace std;
// ------------------------------------------------------------------------ //
//* Kadane's Algorithm
void func1(int arr[], int n)
{
int curr_sum =arr[0];
int max_sum = INT_MIN;
for(int i=1; i<n; i++)
{
if(curr_sum<0)
curr_sum=0;
curr_sum+=arr[i];
max_sum = max(max_sum, curr_sum);
}
cout<<"Max sum using Kadane's Approach: "<<max_sum<<endl;
}
// ------------------------------------------------------------------------ //
//* Cumulative Sum Approach
// reduce time complexity
void func2(int arr[],int n)
{
int cumm_sum[n+1];
cumm_sum[0] = 0;
for(int i=1; i<=n; i++)
cumm_sum[i] = cumm_sum[i-1] + arr[i-1];
int max_sum = INT_MIN;
int sum =0;
for(int i=1; i<=n; i++)
{
sum=0;
for(int j=0; j<i; j++)
{
sum = cumm_sum[i] - cumm_sum[j];
max_sum = max(max_sum, sum);
}
}
cout<<"Max Sum using cumulative approach: "<<max_sum<<endl;
}
// ------------------------------------------------------------------------ //
void func3(int arr[], int n)
{
int sum=0;
int max_sum=INT_MIN;
for(int i=0; i<n; i++)
{
for(int j=i; j<n; j++)
{
sum=0;
for(int k=i; k<=j; k++)
{
sum+=arr[k];
}
max_sum = max(sum,max_sum);
}
}
cout<<"Max Sum using normal approach: "<<max_sum<<endl;
}
// ------------------------------------------------------------------------ //
int main()
{
cout<<"Length of array: "<<endl;
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
func1(arr,n);
func2(arr,n);
func3(arr,n);
return 0;
}