|
| 1 | +# Kadane's Algorithm |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | +Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. |
| 5 | +Note: A subarray is a contiguous part of an array. |
| 6 | + |
| 7 | + |
| 8 | +1st example |
| 9 | + |
| 10 | +```text |
| 11 | +
|
| 12 | +Input: nums = [-2,1,-3,4,-1,2,1,-5,4] |
| 13 | +Output: 6 |
| 14 | +Explanation: [4,-1,2,1] has the largest sum = 6. |
| 15 | +``` |
| 16 | + |
| 17 | +2nd example |
| 18 | + |
| 19 | +```text |
| 20 | +
|
| 21 | +Input: nums = [5,4,-1,7,8] |
| 22 | +Output: 23 |
| 23 | +Explanation: [5,4,-1,7,8] has the largest sum = 23. |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +## Time Complexity |
| 28 | + |
| 29 | +O(N) |
| 30 | + |
| 31 | +## Space Complexity |
| 32 | + |
| 33 | +O(1) |
| 34 | + |
| 35 | +## Algorithm |
| 36 | +The idea is to keep track of the maximum sum of the subarray ending at the current index. |
| 37 | +The maximum sum of the subarray ending at the current index can be calculated by adding the current element to the maximum sum of the subarray ending at the previous index. |
| 38 | +If the maximum sum of the subarray ending at the previous index is negative, then the maximum sum of the subarray ending at the current index is the current element. |
| 39 | +The maximum sum of the subarray ending at the current index is stored in a variable maxEndingHere. |
| 40 | +The maximum sum of the subarray ending at the current index is compared with the maximum sum of the subarray ending at the previous index. |
| 41 | +If the maximum sum of the subarray ending at the current index is greater than the maximum sum of the subarray ending at the previous index, then the maximum sum of the subarray ending at the current index is stored in a variable maxSoFar. |
| 42 | +The maximum sum of the subarray ending at the current index is returned as the output. |
| 43 | + |
| 44 | + |
| 45 | +## Walkthrough |
| 46 | + |
| 47 | +`array = [-2,1,-3,4,-1,2,1,-5,4]` |
| 48 | + |
| 49 | +We consider each element of the array and decide whether to include it in the current subarray or start a new subarray from it. |
| 50 | + |
| 51 | +### Calculation Formula for two variables |
| 52 | + |
| 53 | +``` |
| 5
10000
4 | +current Sum = 0 Calculated by max(number, currentSum + number) |
| 55 | +largest Sum = float("-inf") Calculated by max(currentSum, largestSum) |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +### Traversing through an array will look like |
| 60 | + |
| 61 | +``` |
| 62 | +Consider -2 |
| 63 | +current_sum = max(-2, current_sum + -2) = max(-2, 0 + -2) = -2 |
| 64 | +largest_sum = max(-2, float("-inf")) = -2 |
| 65 | +``` |
| 66 | +``` |
| 67 | +Consider 1 |
| 68 | +current_sum = max(1, current_sum+1) = max(1, -2+1) = 1 |
| 69 | +largest_sum = max(1, -2) = 1 |
| 70 | +``` |
| 71 | +``` |
| 72 | +Consider -3 |
| 73 | +current_sum = max(-3, current_sum + -3) = max(-3, 1 + -3) = -2 |
| 74 | +largest_sum = max(-2, 1) = 1 |
| 75 | +``` |
| 76 | +``` |
| 77 | +Consider 4 |
| 78 | +current_sum = max(4, current_sum + 4) = max(4, -2 + 4) = 4 |
| 79 | +largest_sum = max(4, 1) = 4 |
| 80 | +``` |
| 81 | +``` |
| 82 | +Consider -1 |
| 83 | +current_sum = max(-1, current_sum + -1) = max(-1, 4 + -1) = 3 |
| 84 | +largest_sum = max(3, 4) = 4 |
| 85 | +``` |
| 86 | +``` |
| 87 | +Consider 2 |
| 88 | +current_sum = max(2, current_sum + 2) = max(2, 3 + 2) = 5 |
| 89 | +largest_sum = max(5, 4) = 5 |
| 90 | +``` |
| 91 | +``` |
| 92 | +Consider 1 |
| 93 | +current_sum = max(1, current_sum + 1) = max(1, 5 + 1) = 6 |
| 94 | +largest_sum = max(6, 5) = 6 |
| 95 | +``` |
| 96 | +``` |
| 97 | +Consider -5 |
| 98 | +current_sum = max(-5, current_sum + -5) = max(-5, 6 + -5) = 1 |
| 99 | +largest_sum = max(1, 6) = 6 |
| 100 | +``` |
| 101 | +``` |
| 102 | +Consider 4 |
| 103 | +current_sum = max(4, current_sum + 4) = max(4, 1 + 4) = 5 |
| 104 | +largest_sum = max(5, 6) = 6 |
| 105 | +``` |
| 106 | + |
| 107 | +```Hence the output will be 6``` |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +### Code Implementation Links |
| 112 | + |
| 113 | +- [Python](https://github.com/TheAlgorithms/Python/blob/252df0a149502143a14e7283424d40b785dd451c/maths/kadanes.py) |
0 commit comments