|
| 1 | +<h1 align='center'>Maximum - Subarray</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) |
| 6 | + |
| 7 | + |
| 8 | +## Problem Explanation |
| 9 | +The problem asks to find the contiguous subarray (containing at least one number) within a given array of integers `nums` that has the largest sum. |
| 10 | + |
| 11 | +- **Input**: An integer array `nums`. |
| 12 | +- **Output**: The maximum sum of a subarray. |
| 13 | + |
| 14 | +#### **Example 1**: |
| 15 | +For the array `[-2, 1, -3, 4, -1, 2, 1, -5, 4]`: |
| 16 | +- The contiguous subarray `[4, -1, 2, 1]` has the largest sum, which is `6`. |
| 17 | + |
| 18 | +#### **Example 2**: |
| 19 | +For the array `[1]`: |
| 20 | +- The only element is `1`, so the largest subarray sum is `1`. |
| 21 | + |
| 22 | +#### **Approach**: |
| 23 | + |
| 24 | +To solve this problem efficiently, we use **Kadane's Algorithm**, which allows us to find the maximum sum of a subarray in linear time, \(O(n)\), where \(n\) is the number of elements in the array. |
| 25 | + |
| 26 | +**Kadane's Algorithm Idea**: |
| 27 | +1. We traverse through the array while maintaining two variables: |
| 28 | + - `sum`: This tracks the sum of the current subarray. It is updated as we move through the array. |
| 29 | + - `maxi`: This stores the maximum sum encountered so far. |
| 30 | + |
| 31 | +2. At each step: |
| 32 | + - Add the current element to `sum`. |
| 33 | + - If the sum exceeds `maxi`, update `maxi`. |
| 34 | + - If `sum` becomes negative, reset `sum` to 0 (because continuing with a negative sum would reduce the overall result). |
| 35 | + |
| 36 | +#### **Why Kadane's Algorithm works**: |
| 37 | +- If at any point the sum of the subarray becomes negative, it means the subarray is doing more harm than good in contributing to the maximum sum. Therefore, we reset the sum and start fresh from the next element. |
| 38 | + |
| 39 | +## Problem Solution |
| 40 | +```cpp |
| 41 | +class Solution { |
| 42 | +public: |
| 43 | + int maxSubArray(vector<int>& nums) { |
| 44 | + int sum = 0; |
| 45 | + int maxi = nums[0]; |
| 46 | + |
| 47 | + for(int i = 0; i < nums.size(); i++){ |
| 48 | + sum += nums[i]; |
| 49 | + maxi = max(maxi, sum); |
| 50 | + |
| 51 | + if(sum < 0) sum = 0; |
| 52 | + } |
| 53 | + |
| 54 | + return maxi; |
| 55 | + } |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +## Problem Solution Explanation |
| 60 | + |
| 61 | +1. **`int sum = 0;`** |
| 62 | + - This variable keeps track of the sum of the current subarray. |
| 63 | + - Initially, we start with `sum = 0`, because we are starting from the beginning of the array. |
| 64 | + |
| 65 | +2. **`int maxi = nums[0];`** |
| 66 | + - This variable keeps track of the maximum subarray sum found so far. |
| 67 | + - We initialize it to the first element of the array because, at the beginning, the maximum sum is at least the first element. |
| 68 | + |
| 69 | +3. **`for (int i = 0; i < nums.size(); i++) {`** |
| 70 | + - A loop to iterate through each element of the array. |
| 71 | + |
| 72 | +4. **`sum += nums[i];`** |
| 73 | + - We add the current element `nums[i]` to the `sum` of the current subarray. |
| 74 | + - This accumulates the sum of elements for the current subarray we're considering. |
| 75 | + |
| 76 | +5. **`maxi = max(maxi, sum);`** |
| 77 | + - After updating the `sum`, we check if this sum is larger than the previously recorded maximum (`maxi`). |
| 78 | + - If `sum` is larger, update `maxi` with the new value. |
| 79 | + |
| 80 | +6. **`if (sum < 0) sum = 0;`** |
| 81 | + - If the `sum` becomes negative, we reset `sum` to 0 because continuing with a negative sum would decrease the overall result. |
| 82 | + - We effectively discard any subarray with a negative sum and start from the next element. |
| 83 | + |
| 84 | +7. **`return maxi;`** |
| 85 | + - Finally, we return `maxi`, which holds the largest sum of a contiguous subarray in the given array. |
| 86 | + |
| 87 | +### **Step 3: Example Walkthrough** |
| 88 | + |
| 89 | +Let's walk through the example with the array `[-2, 1, -3, 4, -1, 2, 1, -5, 4]`. |
| 90 | + |
| 91 | +1. **Initialization**: |
| 92 | + - `sum = 0` |
| 93 | + - `maxi = -2` (because the first element is `-2`) |
| 94 | + |
| 95 | +2. **Iteration through the array**: |
| 96 | + |
| 97 | + - **i = 0** (current element `-2`): |
| 98 | + - `sum = sum + (-2) = 0 + (-2) = -2` |
| 99 | + - `maxi = max(-2, -2) = -2` |
| 100 | + - `sum` is negative, so we reset `sum = 0`. |
| 101 | + |
| 102 | + - **i = 1** (current element `1`): |
| 103 | + - `sum = sum + 1 = 0 + 1 = 1` |
| 104 | + - `maxi = max(-2, 1) = 1` |
| 105 | + |
| 106 | + - **i = 2** (current element `-3`): |
| 107 | + - `sum = sum + (-3) = 1 + (-3) = -2` |
| 108 | + - `maxi = max(1, -2) = 1` |
| 109 | + - `sum` is negative, so we reset `sum = 0`. |
| 110 | + |
| 111 | + - **i = 3** (current element `4`): |
| 112 | + - `sum = sum + 4 = 0 + 4 = 4` |
| 113 | + - `maxi = max(1, 4) = 4` |
| 114 | + |
| 115 | + - **i = 4** (current element `-1`): |
| 116 | + - `sum = sum + (-1) = 4 + (-1) = 3` |
| 117 | + - `maxi = max(4, 3) = 4` |
| 118 | + |
| 119 | + - **i = 5** (current element `2`): |
| 120 | + - `sum = sum + 2 = 3 + 2 = 5` |
| 121 | + - `maxi = max(4, 5) = 5` |
| 122 | + |
| 123 | + - **i = 6** (current element `1`): |
| 124 | + - `sum = sum + 1 = 5 + 1 = 6` |
| 125 | + - `maxi = max(5, 6) = 6` |
| 126 | + |
| 127 | + - **i = 7** (current element `-5`): |
| 128 | + - `sum = sum + (-5) = 6 + (-5) = 1` |
| 129 | + - `maxi = max(6, 1) = 6` |
| 130 | + |
| 131 | + - **i = 8** (current element `4`): |
| 132 | + - `sum = sum + 4 = 1 + 4 = 5` |
| 133 | + - `maxi = max(6, 5) = 6` |
| 134 | + |
| 135 | +3. **Final Output**: After the iteration, the maximum subarray sum is `6`. |
| 136 | + |
| 137 | +### **Step 4: Time and Space Complexity** |
| 138 | + |
| 139 | +#### **Time Complexity**: |
| 140 | +- The algorithm iterates through the entire array once, making the time complexity \(O(n)\), where \(n\) is the size of the array. |
| 141 | + |
| 142 | +#### **Space Complexity**: |
| 143 | +- The algorithm uses a constant amount of extra space (only two variables, `sum` and `maxi`), so the space complexity is \(O(1)\). |
| 144 | + |
| 145 | +### **Step 5: Additional Recommendations** |
| 146 | + |
| 147 | +1. **Practice Edge Cases**: |
| 148 | + - Single element arrays. |
| 149 | + - Arrays where all elements are negative. |
| 150 | + - Arrays with mixed positive and negative numbers. |
| 151 | + |
| 152 | +2. **Visualize the Process**: |
| 153 | + - Try drawing or printing the `sum` and `maxi` at each step to better understand how Kadane's Algorithm works. |
| 154 | + |
| 155 | +3. **Improve Problem Solving**: |
| 156 | + - Practice more problems related to dynamic programming and sliding window techniques, as they often involve similar principles. |
| 157 | + |
| 158 | +By understanding Kadane's Algorithm and its implementation, beginners can efficiently solve problems related to maximum subarrays and build on this knowledge for more advanced dynamic programming problems. |
0 commit comments