-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathKadane.cpp
60 lines (60 loc) · 1.64 KB
/
Kadane.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
#include <bits/stdc++.h>
using namespace std;
// Kadane Algorithm is used to find Largest Sum Contiguous Subarray
// It works in O(N) Time Complexity and O(1) Space Complexity
class Kadane
{
public:
int maxSubArray(vector<int> &nums)
{
// this variable is used to capture the sum at each iteration
int sum = 0;
// this is used to store the maximum sum received in each iteration
int maxi = 0;
// Travelling the whole array
for (int i = 0; i < nums.size(); i++)
{
// Adding current element to the sum
sum = sum + nums[i];
// comparing it with the max sum till now
maxi = max(maxi, sum);
// if the sum becomes negative then there is no point in moving forward with that subarray so updated sum=0 for next iterations
if (sum < 0)
{
sum = 0;
}
}
// returning the max sum
return maxi;
}
};
int main()
{
// size of the array
int n;
cout << "Enter the size of the Array :";
cin >> n;
// created the vector
vector<int> v;
// storing the elements
cout << "Enter the Elements :";
while (n--)
{
// taking input from the user
int element;
cin >> element;
v.push_back(element);
}
// creating the object of the class
Kadane obj;
// storing the max_sub_array sum
int max_subarray_sum = obj.maxSubArray(v);
// Printing the result
cout << "Maximum Sub Array sum in :";
for (auto i : v)
{
cout << i << " ";
}
cout << "is: " << max_subarray_sum << endl;
return 0;
}