-
Notifications
You must be signed in to change notification settings - Fork 492
/
MaximumSumSubarray.cpp
106 lines (94 loc) Β· 2.14 KB
/
MaximumSumSubarray.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
101
102
103
104
105
106
#include<iostream>
#include <algorithm>
using namespace std;
//Brute force Approach
int maxSubArraySum1(int a[],int n)
{
int maxSum=INT_MIN;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int sum=0;
for(int k=i;k<=j;k++)
{
sum+=a[k];
}
maxSum = max(maxSum,sum);
}
}
return maxSum;
}
//Cumulative Sum Approach
int maxSubArraySum2(int a[],int n)
{
const int N= n+1;
int currsum[N];
currsum[0] = 0;
int maxSum = INT_MIN;
for(int i=1;i<=n;i++)
{
currsum[i] = currsum[i-1] + a[i-1];
}
for(int i=1;i<=n;i++)
{
int sum=0;
for(int j=0;j<i;j++)
{
sum = currsum[i] - currsum[j];
maxSum = max(sum,maxSum);
}
}
return maxSum;
}
//kadene's algorithm
int maxSubArraySum3(int a[],int n)
{
int currsum=0;
int maxSum=INT_MIN;
for(int i=0;i<n;i++)
{
currsum+=a[i];
if(currsum<0)//keep on updting currsum if its less than zero.
{
currsum=0;
}
maxSum=max(maxSum,currsum);
}
return maxSum;
}
int main() {
int n;
cout<<"Enter the size of an array: ";
cin>>n;
int a[n];
//taking input of array from user
cout<<"Enter values of an array: ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int x;
cout<<"-----------CHOICES-----------\n1. Brute Force Approach \n2. Cumulative Sum Approach \n3. kadene's algorithm \n4. Exit \n"<<endl;
cout<<"Enter the choice: ";
cin>>x;
switch (x) {
case 1:
cout << "Time Complexity O(n^3)"<<endl;
cout << "Maximum contiguous sum is " <<maxSubArraySum1(a, n)<<endl;
break;
case 2:
cout << "Time Complexity O(n^2)"<<endl;
cout << "Maximum contiguous sum is " <<maxSubArraySum2(a, n)<<endl;
break;
case 3:
cout << "Time Complexity O(n)"<<endl;
cout << "Maximum contiguous sum is " <<maxSubArraySum3(a, n)<<endl;
case 4:
exit(x);
default:
cout << "Wrong Choice";
break;
}
return 0;
}