Skip to content

Commit 985eef6

Browse files
Merge pull request #195 from harshendram/Algo/KadaneAlgo
Added the implementation of kadane algorithm implemented in cpp
2 parents 3b4a8c3 + 20f6739 commit 985eef6

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include <climits>
4+
#include <iostream>
5+
6+
int kadane(std::vector<int> &nums)
7+
{
8+
if (nums.empty())
9+
{
10+
return 0;
11+
}
12+
13+
int max_so_far = INT_MIN;
14+
int max_ending_here = 0;
15+
16+
for (int num : nums)
17+
{
18+
max_ending_here = max_ending_here + num;
19+
20+
if (max_so_far < max_ending_here)
21+
{
22+
max_so_far = max_ending_here;
23+
}
24+
25+
if (max_ending_here < 0)
26+
{
27+
max_ending_here = 0;
28+
}
29+
}
30+
31+
if (max_so_far == INT_MIN)
32+
{
33+
int max_val = nums[0];
34+
for (size_t i = 1; i < nums.size(); ++i)
35+
{
36+
if (nums[i] > max_val)
37+
{
38+
max_val = nums[i];
39+
}
40+
}
41+
return max_val;
42+
}
43+
44+
return max_so_far;
45+
}
46+
47+
// A slightly cleaner version that handles all-negative arrays without a separate check.
48+
int kadane_optimized(std::vector<int> &nums)
49+
{
50+
if (nums.empty())
51+
{
52+
return 0;
53+
}
54+
55+
int max_so_far = nums[0];
56+
int current_max = nums[0];
57+
58+
for (size_t i = 1; i < nums.size(); ++i)
59+
{
60+
current_max = std::max(nums[i], current_max + nums[i]);
61+
max_so_far = std::max(max_so_far, current_max);
62+
}
63+
64+
return max_so_far;
65+
}
66+
67+
int main()
68+
{
69+
// Test case 1: Mixed positive and negative numbers
70+
std::vector<int> arr1 = {-2, -3, 4, -1, -2, 1, 5, -3};
71+
std::cout << "Array 1: [-2, -3, 4, -1, -2, 1, 5, -3]" << std::endl;
72+
std::cout << "Maximum subarray sum (kadane): " << kadane(arr1) << std::endl;
73+
std::cout << "Maximum subarray sum (optimized): " << kadane_optimized(arr1) << std::endl;
74+
std::cout << std::endl;
75+
76+
// Test case 2: All negative numbers
77+
std::vector<int> arr2 = {-5, -2, -8, -1};
78+
std::cout << "Array 2: [-5, -2, -8, -1]" << std::endl;
79+
std::cout << "Maximum subarray sum (kadane): " << kadane(arr2) << std::endl;
80+
std::cout << "Maximum subarray sum (optimized): " << kadane_optimized(arr2) << std::endl;
81+
std::cout << std::endl;
82+
83+
// Test case 3: All positive numbers
84+
std::vector<int> arr3 = {1, 2, 3, 4, 5};
85+
std::cout << "Array 3: [1, 2, 3, 4, 5]" << std::endl;
86+
std::cout << "Maximum subarray sum (kadane): " << kadane(arr3) << std::endl;
87+
std::cout << "Maximum subarray sum (optimized): " << kadane_optimized(arr3) << std::endl;
88+
std::cout << std::endl;
89+
90+
// Test case 4: Single element
91+
std::vector<int> arr4 = {-3};
92+
std::cout << "Array 4: [-3]" << std::endl;
93+
std::cout << "Maximum subarray sum (kadane): " << kadane(arr4) << std::endl;
94+
std::cout << "Maximum subarray sum (optimized): " << kadane_optimized(arr4) << std::endl;
95+
std::cout << std::endl;
96+
97+
return 0;
98+
}

0 commit comments

Comments
 (0)