Maximum Product Sub-array (LeetCode 152) | Full Solution with animations and proof | Simplified

  Рет қаралды 37,198

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 123
@nikoo28
@nikoo28 5 ай бұрын
As of this writing LeetCode added a new test case which cause the above code to fail. The logic is still correct though. The test case fails because of overflow error. With just a small fix, the code still works. Have a look at the updated code on my Github link :)
@Armaan_Punia_
@Armaan_Punia_ 4 ай бұрын
[0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0] this is the testcase for which it fails; can you tell what the fix is sir?
@nikoo2805
@nikoo2805 4 ай бұрын
@@Armaan_Punia_that is what I exactly did in the updated code. Did you have a look?
@Armaan_Punia_
@Armaan_Punia_ 4 ай бұрын
@@nikoo2805 yesterday I had removed what I thought was redundant, 190/191 test cases then passed, now all did! thank you
@katerghost642
@katerghost642 4 ай бұрын
@@Armaan_Punia_ bhai iska answer mille toh reply krdena salla do ghanta ho gya h
@unknownman1
@unknownman1 4 ай бұрын
@@katerghost642 class Solution { public int maxProduct(int[] nums) { long leftproduct = 1; long rightproduct = 1; long ans = nums[0]; for(int i=0; i
@yashyadav7017
@yashyadav7017 5 ай бұрын
bhai tum kya bande ho yaar… when other top teachers and youtubers fail to explain the hardest problems, you explain those problems so easily somehow….. Man you deserve so much appreciation… Great work bhai..so much inspired by you… Really top-notch explanations.
@nikoo28
@nikoo28 4 ай бұрын
that is so kind of you...please share and support as much as you can :)
@sayedmahmudulalam2738
@sayedmahmudulalam2738 Жыл бұрын
This is the simplest and most intutive solution.
@MedhatR-do9le
@MedhatR-do9le Ай бұрын
the way you explain begining of cases like, all postive to odd and even negatives unitl the end is awesome.. the best simple explantaion of that algorithm.
@andreabad3518
@andreabad3518 9 ай бұрын
I was really struggling to understand some other solutions for this problem that used dynamic programming, but you really made it simple to understand! Thanks!
@harshithreddy-w2g
@harshithreddy-w2g 9 ай бұрын
this is the best simple solution even the editorial solution is nothing infront of this
@nikoo28
@nikoo28 8 ай бұрын
glad you feel that way!!
@abdulgaffarabdulmalik4333
@abdulgaffarabdulmalik4333 Жыл бұрын
My question is, why does it work?
@nikoo28
@nikoo28 Жыл бұрын
please elaborate...
@billyfigueroa1617
@billyfigueroa1617 9 ай бұрын
😂
@unemployedcse3514
@unemployedcse3514 6 ай бұрын
answer to ur question is if u observe carefully traversing from left and right indirectly we are calculating product of all sub array and each time storing max subbarray product
@ghanashrim2839
@ghanashrim2839 Жыл бұрын
Your explanations are so on point yaar... I've been binge-watching all your videos...! Please make more.
@nikoo28
@nikoo28 Жыл бұрын
Thank you so much 😀
@dipteshdeore2935
@dipteshdeore2935 6 ай бұрын
Clean , crisp & most underrated !
@tejasavhad4010
@tejasavhad4010 Жыл бұрын
Underrated channel.
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
the way you explain things are just Awesome.
@MinhLe-ks5en
@MinhLe-ks5en 8 ай бұрын
This is genius. You deserve more likes and more subscribers. I love your systematic way of explaining the reasoning.
@nikoo28
@nikoo28 4 ай бұрын
Glad you think so!
@tripletgamingbuddies5365
@tripletgamingbuddies5365 3 ай бұрын
for those whose code fails at 191 testcase use the below code class Solution { public int maxProduct(int[] nums) { double maxpro=nums[0],right=1,left=1; for(int i=0;i
@subhadippahari185
@subhadippahari185 Жыл бұрын
What an explanation. Really mind-blowing. Saw other videos on the same topic, but this is a really whole different level of logic.
@hoddybhaba6704
@hoddybhaba6704 Жыл бұрын
Fantastic explaination👏👏👏You deserve more likes, more views and definitely more subscribers!!!
@NikhilRaj-wv6nf
@NikhilRaj-wv6nf Ай бұрын
you are so good man, I used to watch Striver's lecture but for revision Im watching you:)
@faizuddin4032
@faizuddin4032 10 ай бұрын
Very clean , clear and simple explanation.
@lifesucks5091
@lifesucks5091 Ай бұрын
very good explanation.This channel's gonna explode with subscribers pretty soon
@anonymous13378
@anonymous13378 5 ай бұрын
Too concise... Too the point.... Best explanation.... I can compare you with Striver... Though you are better than him at many places.... ❤
@nikoo28
@nikoo28 5 ай бұрын
Thanks for the view
@Max-tq1ig
@Max-tq1ig Жыл бұрын
Hi, Nikhil! I understand the code and how can you get the answer by using the two comparisons from right and left, but I don't get the logical idea of pointing out that you need to remove the middle number at minute 6:26. Is it just an example of what should we get if you keep with odds numbers instead of an even; or just the idea of having more numbers in general? I don't understand x__x It's not something to will do automatically by following the code?
@nikoo28
@nikoo28 Жыл бұрын
you don't have to remove the middle number, we are just trying to get the maximum product. 2 negatives will give you a positive number, but 3 negatives will give you a negative number again.
@yashbhinwal2801
@yashbhinwal2801 Жыл бұрын
you did a great job explaining this concept, really helpful!!
@jatinkumar4410
@jatinkumar4410 7 ай бұрын
amazing explanation... Thank you
@stanislavmozolevskiy8346
@stanislavmozolevskiy8346 6 ай бұрын
I do really like your solution, it is so much easier to understand. Thank you
@akhintheruvath
@akhintheruvath 4 ай бұрын
Crisp and clear explanation. Thank you bro.
@jironymojirolamus913
@jironymojirolamus913 4 ай бұрын
Great explanation, thanks! I actually solved it in a similar way, but was unsure if it's a way to go since everyone is talking DP nowadays.
@yadneshraut6854
@yadneshraut6854 5 ай бұрын
Understood this logic but it fails at last testcase of leetcode 152 so please update the logic for the testcase and code too........!!! Thank you !!
@nikoo28
@nikoo28 4 ай бұрын
since I cannot edit the video, the code has been updated in the github link :)
@yadneshraut6854
@yadneshraut6854 4 ай бұрын
@@nikoo28 thank you sir !
@rajalakshmirajasekar260
@rajalakshmirajasekar260 Жыл бұрын
Bro ur explanation is awesome!! I am able to think the flow of program and build the intuition easily.. one request bro Plz upload recursion vdos.. Thank you!!
@nikoo28
@nikoo28 Жыл бұрын
i have made several videos that use recursion. Check out the basics here: kzbin.info/www/bejne/fIW3eZ6jo9utoq8
@shreyanshgoel9757
@shreyanshgoel9757 6 ай бұрын
Your solution impies that ans subarray lie either in suffix or in prefix in non zero elements are present
@maggiesayabie
@maggiesayabie Жыл бұрын
I love your explanation!
@user-he3hs6iy8o
@user-he3hs6iy8o 5 ай бұрын
it fails on the last test case on leetcode
@nikoo28
@nikoo28 4 ай бұрын
since I cannot edit the video, the code has been updated in the github link :)
@cagarwal
@cagarwal Жыл бұрын
Can you explain why we need to start from both sides? I'm not able to see any benefit in starting from both sides as both eventually gives the same result.
@nikoo28
@nikoo28 Жыл бұрын
I don't understand...did you go though my complete video? It cannot be done if we don't go from both sides.
@CyberZeo
@CyberZeo 2 ай бұрын
Thank you so much for this simplest explanation
@gopik6202
@gopik6202 5 ай бұрын
class Solution { public int maxProduct(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int maxProduct = nums[0]; int currentMax = nums[0]; int currentMin = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { // Swap currentMax and currentMin when encountering a negative number int temp = currentMax; currentMax = currentMin; currentMin = temp; } currentMax = Math.max(nums[i], currentMax * nums[i]); currentMin = Math.min(nums[i], currentMin * nums[i]); maxProduct = Math.max(maxProduct, currentMax); } return maxProduct; } } this is crt logic
@rahulchandak2772
@rahulchandak2772 4 ай бұрын
The logic sounds very simple but its difficult to understand for cases where the subarray does not contain the starting (leftmost) or the ending (rightmost) element. When the sub array is in between the array
@samiramousaie1158
@samiramousaie1158 Жыл бұрын
your explanation is awesome. I totally understand it.
@nikoo28
@nikoo28 Жыл бұрын
Awesome, thank you!
@ashoksaipudi6999
@ashoksaipudi6999 Жыл бұрын
Hey actually its such a good explanation. but one mistake in explanation of 2nd testcase(2:10 min) there you don't take the contigous sub-array.
@nikoo28
@nikoo28 Жыл бұрын
i do take the contiguous sub-array
@powerwithin1367
@powerwithin1367 5 ай бұрын
wrong hai solution leetcode per 190 testcase fail ho raha
@unknownman1
@unknownman1 5 ай бұрын
they have updated the test cases.
@nikoo28
@nikoo28 5 ай бұрын
thanks for letting me know...I will update the code accordingly
@thakur6030
@thakur6030 2 ай бұрын
i think at 12:40 how do you update leftproduct to 2*3 it should be 1*nums[i] i.e-1*2=2 and same for the right product you doing is to be appered in the second iteration
@oknokok
@oknokok 4 ай бұрын
simple and exellent explanation
@shushanbalayan6267
@shushanbalayan6267 Жыл бұрын
Great explanation! Thank you
@amitmandal5842
@amitmandal5842 11 ай бұрын
Excellent explanation. I mean, you made question easy.. valaaaah😊
@nikoo28
@nikoo28 11 ай бұрын
Glad to hear that
@likhithadoddi4885
@likhithadoddi4885 Ай бұрын
Sir your explanation is awesome 😎
@acceleratedofficial1576
@acceleratedofficial1576 6 ай бұрын
Failed or -1, 0,-3
@jiddu99
@jiddu99 5 ай бұрын
How ? It passed i got 0
@nikoo28
@nikoo28 4 ай бұрын
yes, it will pass...and you will get 0
@maherworld1
@maherworld1 Жыл бұрын
AWESOME! God bless u
@nikoo28
@nikoo28 Жыл бұрын
Thank you! You too!
@avinashabhi95
@avinashabhi95 Жыл бұрын
Your explanation is superb. Subscribed.
@nikoo28
@nikoo28 Жыл бұрын
Welcome aboard! 😄
@vishwash7416
@vishwash7416 Ай бұрын
Great explanation loved it
@bharadwajrss8376
@bharadwajrss8376 11 ай бұрын
Hey... can we solve similar kind of question , finding maximum sum of subarray in given array. It usually solved using kadane's algorithm, but can we use this approach? Any way superb explanation ....
@nikoo28
@nikoo28 10 ай бұрын
give me a link to the question.
@hitghitg8747
@hitghitg8747 7 ай бұрын
Thank you my friend simple and easy
@Rfcs1.6
@Rfcs1.6 3 ай бұрын
this code not run in leetcode
@AnzysViews
@AnzysViews 26 күн бұрын
thanks sir
@KhyleSewpersaud
@KhyleSewpersaud 4 ай бұрын
fantastic solution
@240SX2pAc
@240SX2pAc Жыл бұрын
Wonderful solution, thank you
@ashwinnair1035
@ashwinnair1035 8 ай бұрын
Great explanation sir
@atifakhtar8828
@atifakhtar8828 Ай бұрын
amazing video sir loved that
@kesharwani.prashant
@kesharwani.prashant 10 ай бұрын
Looks like stiver copied your exact logic:) Thanks for explaining it so clearly !
@abba5102
@abba5102 7 ай бұрын
Very good brother. Thank you.
@sripoojitha4887
@sripoojitha4887 10 ай бұрын
🎉Great Explaination 😮
@TanishaVarshney-ti9yl
@TanishaVarshney-ti9yl 8 ай бұрын
i understand the question and solution everything completely but on submitting it on leetcode after checking it twice it says wrong answer don't know why could you please tell me?
@nikoo28
@nikoo28 8 ай бұрын
Check out the complete code on my github. Link in description. That should give you a hint.
@TanishaVarshney-ti9yl
@TanishaVarshney-ti9yl 8 ай бұрын
@@nikoo28 i have tried that too but its also not working
@TanishaVarshney-ti9yl
@TanishaVarshney-ti9yl 8 ай бұрын
@@nikoo28please check it out class Solution { public int longestConsecutive(int[] nums) { int n = nums.length; int left=1; int right =1; int ans=nums[0]; for(int i=0;i
@ankitvyas8534
@ankitvyas8534 5 ай бұрын
what if there is no positive element in the array?
@sunshineandrainbow5453
@sunshineandrainbow5453 3 ай бұрын
OG teacher ❤
@RajiurRahman1
@RajiurRahman1 3 ай бұрын
Very simple and easy to understand. I was following another solution which I found a bit difficult to process. Thank you, keep up the good work.
@likhithadoddi4885
@likhithadoddi4885 Ай бұрын
❤❤ love itt sir
@telugustatuscreator8327
@telugustatuscreator8327 9 ай бұрын
How are you getting ideas like this?
@nikoo28
@nikoo28 8 ай бұрын
practice, practice, practice and a lot of practice my friend :)
@tanugiri532
@tanugiri532 8 ай бұрын
Thankyou sir ❤
@Shrutimmmmm
@Shrutimmmmm 6 ай бұрын
U shud also include bruteforce solutions
@jnayehsirine6222
@jnayehsirine6222 5 ай бұрын
I LOVE UR CHANEL
@jd1901-s
@jd1901-s 3 ай бұрын
op explanation👌
@AnishRanjan-p8u
@AnishRanjan-p8u Жыл бұрын
What if all elements are zero. Then it will fail?
@nikoo28
@nikoo28 11 ай бұрын
It won’t fail. The answer will be 0
@VIJAYMAMORIA-c8m
@VIJAYMAMORIA-c8m 5 ай бұрын
Code Failed for nums =[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, -10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0] Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range. Below code changes will fix the issue. long leftProduct = 1; long rightProduct = 1; long ans = nums[0]; leftProduct = (leftProduct == 0 || leftProduct < Integer.MIN_VALUE) ? 1 : leftProduct; rightProduct = (rightProduct == 0 || rightProduct < Integer.MIN_VALUE) ? 1 : rightProduct; return (int) ans;
@nikoo28
@nikoo28 5 ай бұрын
yes, that seems to be a newly added test case. I will update the code accordingly. Thanks for your contribution 😄
@kishanmishra7301
@kishanmishra7301 Жыл бұрын
Hello, Your videos are so good and the way you explain the question as well as the the solution is very nice please keep making these videos I have one request can you please explain (3. Longest Substring Without Repeating Characters) this question of leetcode
@nikoo28
@nikoo28 Жыл бұрын
i added this problem to my list... :)
@sagarpanwar5722
@sagarpanwar5722 3 ай бұрын
best
@BABATUNDE-h7d
@BABATUNDE-h7d 5 ай бұрын
You wont pass 1 test case, for that use double instead of int
@nikoo28
@nikoo28 4 ай бұрын
i fixed the code in link
@robertnant1264
@robertnant1264 8 ай бұрын
Hi! First of all thank you for the video. I'm having a hard time understanding why we have to calculate the product going from the left and also going from the right side of the array. Why is it that a traversal from start of array to its end does not work? Thank you in advance
@nikoo28
@nikoo28 8 ай бұрын
did you follow the explanation of the solution, or just looking at the code?
@alisheheryar1770
@alisheheryar1770 6 ай бұрын
Adios nikhil
@n1724k
@n1724k 3 ай бұрын
omg
@shivdeepbisurkar6526
@shivdeepbisurkar6526 Жыл бұрын
Hi, awesome explanation but it's failing for test case nums =[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, -10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0] output=1981284352 using above logic. Expected =1000000000 (as per leetcode)
@nikoo28
@nikoo28 Жыл бұрын
my code passed on leetcode, did you check the one in the video description on github?
@VIJAYMAMORIA-c8m
@VIJAYMAMORIA-c8m 5 ай бұрын
@@nikoo28 Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range.
@nishantparaskar2048
@nishantparaskar2048 5 ай бұрын
@@VIJAYMAMORIA-c8m Did you resolve with any approch
@AnishRanjan-p8u
@AnishRanjan-p8u Жыл бұрын
What if all elements are zero. Then it will fail?
@AnishRanjan-p8u
@AnishRanjan-p8u Жыл бұрын
What if all elements are zero. Then it will fail?
@_shezzy
@_shezzy 6 ай бұрын
It will not fail
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
Unboxing the Raspberry Pi CM5 Development Kit
11:36
Level 2 Jeff
Рет қаралды 21 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 236 М.
Kadane's Algorithm to Maximum Sum Subarray Problem
11:17
CS Dojo
Рет қаралды 722 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 253 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 491 М.
Maximum Product Subarray - Dynamic Programming - Leetcode 152
15:31
How many people are in the changing room? #devil #lilith #funny #shorts
00:39