|
| 1 | +--- |
| 2 | +id: Kadanes-Algorithm |
| 3 | +title: Kadanes Algorithm |
| 4 | +sidebar_label: KadanesAlgorithm |
| 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 | +```py |
| 39 | + |
| 40 | + def max_sub_array(nums): |
| 41 | + max_sum = float('-inf') |
| 42 | + current_sum = 0 |
| 43 | + |
| 44 | + for num in nums: |
| 45 | + # Add the current number to the current_sum |
| 46 | + current_sum += num |
| 47 | + # Update max_sum if current_sum is greater |
| 48 | + max_sum = max(max_sum, current_sum) |
| 49 | + # Reset current_sum to 0 if it drops below 0 |
| 50 | + if current_sum < 0: |
| 51 | + current_sum = 0 |
| 52 | + |
| 53 | + return max_sum |
| 54 | +``` |
| 55 | + |
| 56 | +### C++ |
| 57 | +```cpp |
| 58 | + // Kadane's Algorithm to find the maximum sum of a contiguous subarray |
| 59 | + |
| 60 | + #include <iostream> |
| 61 | + #include <vector> |
| 62 | + #include <algorithm> |
| 63 | + #include <climits> |
| 64 | + |
| 65 | + int maxSubArray(const std::vector<int>& nums) { |
| 66 | + // Initialize max_sum to the lowest possible value and current_sum to 0 |
| 67 | + int max_sum = INT_MIN; |
| 68 | + int current_sum = 0; |
| 69 | + |
| 70 | + for (int num : nums) { |
| 71 | + // Add the current number to the current_sum |
| 72 | + current_sum += num; |
| 73 | + // Update max_sum if current_sum is greater |
| 74 | + max_sum = std::max(max_sum, current_sum); |
| 75 | + // Reset current_sum to 0 if it drops below 0 |
| 76 | + if (current_sum < 0) { |
| 77 | + current_sum = 0; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return max_sum; |
| 82 | + } |
| 83 | +``` |
| 84 | +
|
| 85 | +### Explanation |
| 86 | +Explanation: |
| 87 | +
|
| 88 | +#### Initialization: |
| 89 | +
|
| 90 | + -`max_sum` is initialized to `INT_MIN` to handle arrays with all negative numbers. |
| 91 | +
|
| 92 | + -`current_sum` starts at `0`. |
| 93 | +
|
| 94 | +#### Iteration: |
| 95 | +
|
| 96 | + -Traverse through each number in the array: |
| 97 | + |
| 98 | + -Add the number to `current_sum`. |
| 99 | +
|
| 100 | + -Update `max_sum` to be the maximum of `max_sum` and `current_sum`. |
| 101 | +
|
| 102 | + -If `current_sum` becomes negative, reset it to `0` to start a new subarray. |
| 103 | +
|
| 104 | +
|
0 commit comments