|
| 1 | +# *25. Maximum Product Subarray* |
| 2 | +The problem can be found at the following link: [Problem Link](https://www.geeksforgeeks.org/problems/maximum-product-subarray3604/1) |
| 3 | + |
| 4 | +<div align="center"> |
| 5 | + <h2>✨ LeetCode Problem of the Day (POTD) Started ✨</h2> |
| 6 | +</div> |
| 7 | + |
| 8 | +- As promised in the poll, I’ve started solving and uploading **LeetCode Problem of the Day (POTD)** solutions! 🎯 |
| 9 | +- My latest solution is now live: |
| 10 | + **[773. Sliding Puzzle](https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/773.Sliding%20Puzzle.md)** |
| 11 | + |
| 12 | +<div align="center"> |
| 13 | + <a href="https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/773.Sliding%20Puzzle.md"> |
| 14 | + <img src="https://img.shields.io/badge/LeetCode%20POTD-Solution%20Live-brightgreen?style=for-the-badge&logo=leetcode" alt="LeetCode POTD Solution" /> |
| 15 | + </a> |
| 16 | + <a href="https://github.com/Hunterdii/Leetcode-POTD/blob/main/November%202024%20Leetcode%20Solution/773.Sliding%20Puzzle.md"> |
| 17 | + <img src="https://img.shields.io/badge/Solutions-Up%20to%20Date-blue?style=for-the-badge" alt="Solutions Up-to-Date" /> |
| 18 | +</div> |
| 19 | + |
| 20 | +<br/> |
| 21 | + |
| 22 | +## Problem Description |
| 23 | + |
| 24 | +Given an array `arr[]` that contains both positive and negative integers (and possibly zeros), find the maximum product of any subarray within the array. |
| 25 | + |
| 26 | +**Note:** The result will always fit within the range of a 32-bit integer. |
| 27 | + |
| 28 | +### Examples: |
| 29 | + |
| 30 | +**Input:** |
| 31 | +`arr[] = [-2, 6, -3, -10, 0, 2]` |
| 32 | +**Output:** |
| 33 | +`180` |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | +The subarray with the maximum product is {6, -3, -10} with product = 6 * (-3) * (-10) = 180. |
| 37 | + |
| 38 | +**Input:** |
| 39 | +`arr[] = [-1, -3, -10, 0, 60]` |
| 40 | +**Output:** |
| 41 | +`60` |
| 42 | + |
| 43 | +**Explanation:** |
| 44 | +The subarray with the maximum product is {60}. |
| 45 | + |
| 46 | +**Input:** |
| 47 | +`arr[] = [2, 3, 4]` |
| 48 | +**Output:** |
| 49 | +`24` |
| 50 | + |
| 51 | +**Explanation:** |
| 52 | +For an array with all positive elements, the result is the product of all elements. |
| 53 | + |
| 54 | +### Constraints: |
| 55 | +- `1 ≤ arr.size() ≤ 10^6` |
| 56 | +- `-10 ≤ arr[i] ≤ 10` |
| 57 | + |
| 58 | +## My Approach |
| 59 | + |
| 60 | +1. **Dynamic Programming with Two Variables (maxVal, minVal):** |
| 61 | + The idea is to track the maximum and minimum product subarrays up to the current index. The minimum product may become the maximum product if multiplied by a negative number. |
| 62 | + |
| 63 | +2. **Steps:** |
| 64 | + - Traverse the array, updating the `maxVal` and `minVal` at each step based on the current number. |
| 65 | + - If the current number is negative, we swap `maxVal` and `minVal`. |
| 66 | + - Update `maxProduct` after processing each element to keep track of the overall maximum product encountered. |
| 67 | + |
| 68 | +## Time and Auxiliary Space Complexity |
| 69 | + |
| 70 | +- **Expected Time Complexity:** O(n), where `n` is the size of the array. The algorithm requires a single pass through the array, making it efficient. |
| 71 | +- **Expected Auxiliary Space Complexity:** O(1), as we use a constant amount of extra space. |
| 72 | + |
| 73 | +## Code (C) |
| 74 | + |
| 75 | +```c |
| 76 | +int maxProduct(int arr[], int n) { |
| 77 | + int maxProduct = arr[0]; |
| 78 | + int maxVal = arr[0], minVal = arr[0]; |
| 79 | + |
| 80 | + for (int i = 1; i < n; ++i) { |
| 81 | + if (arr[i] < 0) { |
| 82 | + int temp = maxVal; |
| 83 | + maxVal = minVal; |
| 84 | + minVal = temp; |
| 85 | + } |
| 86 | + |
| 87 | + maxVal = (arr[i] > maxVal * arr[i]) ? arr[i] : maxVal * arr[i]; |
| 88 | + minVal = (arr[i] < minVal * arr[i]) ? arr[i] : minVal * arr[i]; |
| 89 | + |
| 90 | + maxProduct = (maxProduct > maxVal) ? maxProduct : maxVal; |
| 91 | + } |
| 92 | + |
| 93 | + return maxProduct; |
| 94 | +} |
| 95 | +``` |
| 96 | +
|
| 97 | +## Code (Cpp) |
| 98 | +
|
| 99 | +```cpp |
| 100 | +class Solution { |
| 101 | +public: |
| 102 | + int maxProduct(vector<int>& nums) { |
| 103 | + int maxProduct = nums[0]; |
| 104 | + int maxVal = nums[0], minVal = nums[0]; |
| 105 | +
|
| 106 | + for (int i = 1; i < nums.size(); ++i) { |
| 107 | + if (nums[i] < 0) { |
| 108 | + std::swap(maxVal, minVal); |
| 109 | + } |
| 110 | +
|
| 111 | + maxVal = std::max(nums[i], maxVal * nums[i]); |
| 112 | + minVal = std::min(nums[i], minVal * nums[i]); |
| 113 | +
|
| 114 | + maxProduct = std::max(maxProduct, maxVal); |
| 115 | + } |
| 116 | +
|
| 117 | + return maxProduct; |
| 118 | + } |
| 119 | +}; |
| 120 | +``` |
| 121 | + |
| 122 | +<details> |
| 123 | + <summary><h2 align='center'>👨💻 Alternative Approaches </h2></summary> |
| 124 | + |
| 125 | +1) |
| 126 | +```cpp |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + int maxProduct(vector<int>& arr) { |
| 130 | + int n = arr.size(); |
| 131 | + int maxProduct = arr[0], maxVal = arr[0], minVal = arr[0]; |
| 132 | + |
| 133 | + for (int i = 1; i < n; ++i) { |
| 134 | + int current = arr[i]; |
| 135 | + |
| 136 | + if (current < 0) swap(maxVal, minVal); |
| 137 | + |
| 138 | + maxVal = max(current, maxVal * current); |
| 139 | + minVal = min(current, minVal * current); |
| 140 | + |
| 141 | + maxProduct = max(maxProduct, maxVal); |
| 142 | + } |
| 143 | + |
| 144 | + return maxProduct; |
| 145 | + } |
| 146 | +}; |
| 147 | +``` |
| 148 | + |
| 149 | +2) |
| 150 | +```cpp |
| 151 | +class Solution { |
| 152 | +public: |
| 153 | + int maxProduct(vector<int>& arr) { |
| 154 | + int n = arr.size(); |
| 155 | + int maxProduct = arr[0], maxVal = arr[0], minVal = arr[0]; |
| 156 | + |
| 157 | + for (int i = 1; i < n; ++i) { |
| 158 | + int current = arr[i]; |
| 159 | + |
| 160 | + if (current < 0) { |
| 161 | + int temp = maxVal; |
| 162 | + maxVal = minVal; |
| 163 | + minVal = temp; |
| 164 | + } |
| 165 | + |
| 166 | + maxVal = (current > maxVal * current) ? current : maxVal * current; |
| 167 | + minVal = (current < minVal * current) ? current : minVal * current; |
| 168 | + |
| 169 | + if (maxVal > maxProduct) { |
| 170 | + maxProduct = maxVal; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return maxProduct; |
| 175 | + } |
| 176 | +}; |
| 177 | +``` |
| 178 | + |
| 179 | +</details> |
| 180 | + |
| 181 | +## Code (Java) |
| 182 | + |
| 183 | +```java |
| 184 | +class Solution { |
| 185 | + |
| 186 | + int maxProduct(int[] nums) { |
| 187 | + int maxProduct = nums[0]; |
| 188 | + int maxVal = nums[0]; |
| 189 | + int minVal = nums[0]; |
| 190 | + |
| 191 | + for (int i = 1; i < nums.length; i++) { |
| 192 | + if (nums[i] < 0) { |
| 193 | + |
| 194 | + int temp = maxVal; |
| 195 | + maxVal = minVal; |
| 196 | + minVal = temp; |
| 197 | + } |
| 198 | + |
| 199 | + maxVal = Math.max(nums[i], maxVal * nums[i]); |
| 200 | + minVal = Math.min(nums[i], minVal * nums[i]); |
| 201 | + |
| 202 | + maxProduct = Math.max(maxProduct, maxVal); |
| 203 | + } |
| 204 | + |
| 205 | + return maxProduct; |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +## Code (Python) |
| 211 | + |
| 212 | +```python |
| 213 | +class Solution: |
| 214 | + |
| 215 | + def maxProduct(self, arr): |
| 216 | + max_product = arr[0] |
| 217 | + max_val = arr[0] |
| 218 | + min_val = arr[0] |
| 219 | + |
| 220 | + for i in range(1, len(arr)): |
| 221 | + if arr[i] < 0: |
| 222 | + max_val, min_val = min_val, max_val |
| 223 | + |
| 224 | + max_val = max(arr[i], max_val * arr[i]) |
| 225 | + min_val = min(arr[i], min_val * arr[i]) |
| 226 | + |
| 227 | + max_product = max(max_product, max_val) |
| 228 | + |
| 229 | + return max_product |
| 230 | +``` |
| 231 | + |
| 232 | +## Contribution and Support |
| 233 | + |
| 234 | +For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: [Any Questions](https://www.linkedin.com/in/het-patel-8b110525a/). Let’s make this learning journey more collaborative! |
| 235 | + |
| 236 | +⭐ If you find this helpful, please give this repository a star! ⭐ |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +<div align="center"> |
| 241 | + <h3><b>📍Visitor Count</b></h3> |
| 242 | +</div> |
| 243 | + |
| 244 | +<p align="center"> |
| 245 | + <img src="https://profile-counter.glitch.me/Hunterdii/count.svg" /> |
| 246 | +</p> |
0 commit comments