forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kadane_Algorithm.cpp
91 lines (69 loc) · 2.57 KB
/
Kadane_Algorithm.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
#include <bits/stdc++.h>
using namespace std;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define time cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" \
<< "\n";
#define F first
#define S second
#define pb push_back
typedef long long int ll;
/*-----------------------To find Maximum Sum Subarray-------------------------------------------------------------------------------------*/
void solve()
{
int array[] = {4, 3, -2, 6, -14, 7, -1, 4, 5, 7, -10, 2, 9, -10, -5, -9, 6, 1};
int arraySize = sizeof(array) / sizeof(array[0]);
/*****************Brute Force***************************/
int total = array[0];
int current_sum = array[0];
for (int i = 0; i < arraySize; i++)
{
current_sum = array[i];
for (int j = i + 1; j < arraySize; j++)
{
current_sum += array[j];
total = max(current_sum, total);
}
}
/********************Kadane's Approach***********************/
int currentSum = array[0];
int overallSum = array[0];
for (int i = 1; i < arraySize; i++)
{
if (currentSum >= 0)
{
currentSum += array[i];
}
else
{
currentSum = array[i];
}
if (overallSum < currentSum)
{
overallSum = currentSum;
}
}
cout << "Solution using Kadane's Algo : " << overallSum << "\n";
cout << "Solution using Brute Force Approach : " << total << "\n";
/*
Time complexity of Brute force = O(n^2) ;
Time complexity of Kadane's Algorithm = O(n^2) ;
*/
}
int32_t main()
{
fast;
time;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}