Skip to content

Commit cb3c86d

Browse files
Create Kadanes.md
1 parent 21420fa commit cb3c86d

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

docs/dsa/Kadanes.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
id: Algorithms
3+
title: Kadanes Algorithm
4+
sidebar_label: Algorithms
5+
sidebar_position: 2
6+
description: "Kadane's Algorithm efficiently finds the maximum sum contiguous subarray in a one-dimensional array. It's a dynamic programming technique named after its creator, Jay Kadane."
7+
---
8+
## Kadane's Algorithm:
9+
10+
- **Purpose**: Finds the maximum sum of a contiguous subarray.
11+
- **Time Complexity**: O(n)
12+
- **Space Complexity**: O(1)
13+
- **Use Case**: Optimal for solving maximum subarray sum problems in linear time.
14+
### Simple Explanation
15+
Kadane's Algorithm:
16+
17+
1. **Initialize**:
18+
- Set `maxSum` to a very small number (e.g., negative infinity).
19+
- Set `currentSum` to 0.
20+
21+
2. **Iterate Through the Array**:
22+
- For each element in the array:
23+
- Add the element to `currentSum`.
24+
- Update `maxSum` if `currentSum` is greater than `maxSum`.
25+
- If `currentSum` becomes negative, reset it to 0.
26+
27+
3. **Result**:
28+
- The value of `maxSum` at the end of the iteration is the maximum sum of any contiguous subarray.
29+
### Code
30+
31+
Input: arr = {-2,-3,4,-1,-2,1,5,-3}
32+
33+
Output: 7
34+
35+
Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7.
36+
37+
### Python
38+
39+
def max_sub_array(nums):
40+
max_sum = float('-inf')
41+
current_sum = 0
42+
43+
for num in nums:
44+
# Add the current number to the current_sum
45+
current_sum += num
46+
# Update max_sum if current_sum is greater
47+
max_sum = max(max_sum, current_sum)
48+
# Reset current_sum to 0 if it drops below 0
49+
if current_sum < 0:
50+
current_sum = 0
51+
52+
return max_sum
53+
54+
### C++
55+
// Kadane's Algorithm to find the maximum sum of a contiguous subarray
56+
57+
#include <iostream>
58+
#include <vector>
59+
#include <algorithm>
60+
#include <climits>
61+
62+
int maxSubArray(const std::vector<int>& nums) {
63+
// Initialize max_sum to the lowest possible value and current_sum to 0
64+
int max_sum = INT_MIN;
65+
int current_sum = 0;
66+
67+
for (int num : nums) {
68+
// Add the current number to the current_sum
69+
current_sum += num;
70+
// Update max_sum if current_sum is greater
71+
max_sum = std::max(max_sum, current_sum);
72+
// Reset current_sum to 0 if it drops below 0
73+
if (current_sum < 0) {
74+
current_sum = 0;
75+
}
76+
}
77+
78+
return max_sum;
79+
}
80+
81+
### Explanation
82+
Explanation:
83+
84+
#### Initialization:
85+
86+
-`max_sum` is initialized to `INT_MIN` to handle arrays with all negative numbers.
87+
88+
-`current_sum` starts at `0`.
89+
90+
#### Iteration:
91+
92+
-Traverse through each number in the array:
93+
94+
-Add the number to `current_sum`.
95+
96+
-Update `max_sum` to be the maximum of `max_sum` and `current_sum`.
97+
98+
-If `current_sum` becomes negative, reset it to `0` to start a new subarray.
99+
100+

0 commit comments

Comments
 (0)