LeetCode September Challenge(Day-11)

Topic — Maximum Product Subarray
Q. Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
Logic:
- If the length of the array is 0, then we return nothing
- If the length of the array is 1, then we return the array itself.
- Now, in rest of the cases, first we will capture the first element of the array separately and iterate from the second element onward.
- We will multiply the first and second element and capture the product, to compare it with rest of the contiguous sub-array
- Have an eye on the max product with the sub-array
- That’s it
Code:

Happy Coding!!!
Here is the leetcode link:
https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/555/week-2-september-8th-september-14th/3455/
Please feel free to comment, if you have any problem or doubt.
Thank You!!!
Never settle and always Hustle!!!
☺
-Gareeb CODER
(Thanks for your time and do encourage me to write more by clapping.)