Maximum Product Subarray - Best Intuitive Approach Discussed

  Рет қаралды 236,377

take U forward

take U forward

Күн бұрын

Пікірлер: 370
@Iron_Man019
@Iron_Man019 Жыл бұрын
He is a real competitive programmer in all KZbinrs who teaches DSA in India
@lakshsinghania
@lakshsinghania 6 ай бұрын
guys who do cp have better problem solving skills, even doing dsa gives but cp just opens the brain to think in diff solutions
@harshgupta5287
@harshgupta5287 4 ай бұрын
Just a small change in the code for those who are wondering why 191th test case is not passing on leetcode, just change the data type of ans, pre and suff to double so as to prevent overflow in the intermediate steps. It has much bigger range than long and long long. Btw great explaination striver!!
@brp3522
@brp3522 3 ай бұрын
Saved me hours of wondering. Thanks mate appreciate it
@codersoham
@codersoham 3 ай бұрын
Thanks a lot bro. I was searching for this for hours until I saw your comment.
@deepk-007
@deepk-007 3 ай бұрын
Bro is the saviour.
@secondthread-uc9bd
@secondthread-uc9bd 3 ай бұрын
works but cheating lol
@brp3522
@brp3522 3 ай бұрын
@@secondthread-uc9bd How's this cheating dude??
@SHASHANKRUSTAGII
@SHASHANKRUSTAGII Жыл бұрын
I had never seen this approach and the way you explain is like you own it.!!! Superb work striver bro
@takeUforward
@takeUforward Жыл бұрын
;)
@chiragbansod8252
@chiragbansod8252 8 ай бұрын
Completed this playlist also, MAN this is the best teacher of DSA. UNDERSTOOD!!!!!!!
@sasanklakimsetti8967
@sasanklakimsetti8967 Ай бұрын
Great explanantion. Without using dynamic programming and not encountering any overlapping subproblem, he solved it and of course, explained like a boss. And he is. What a man!🙌🙌
@pramodharshav6091
@pramodharshav6091 Жыл бұрын
You're God sent for us to learn in last moment ❤
@AbjSir
@AbjSir Жыл бұрын
Arrays completed Tomorrow I will revise full arrays before starting binary search
@gpraneeth7693
@gpraneeth7693 3 ай бұрын
How
@SusinSunilkumar
@SusinSunilkumar 11 ай бұрын
bro no need any more examples you were the best in explaining this question with the most examples the cases... Really understood the concept very well. Thank you for the video...
@MischievousSoul
@MischievousSoul Жыл бұрын
I was literally searching for the intuitive approach and finally found it.. really appreciate it.. and a great explanation..
@shreyasingh9847
@shreyasingh9847 2 күн бұрын
I am so happy I found your channel because every time when I searched for a solution I would directly get optimized code and I be like how they thought of this, from where did that come in their mind but you build solution making it better and better and it makes things so much better like I can recognize patterns. Thank you so much for your guidance.
@aryanyadav3926
@aryanyadav3926 Жыл бұрын
Brilliant! this is actually much more intuitive as I was also thinking of applying prefix and suffix in it, just couldn't react to the final answer. Thanks.
@messi_codes
@messi_codes Жыл бұрын
Best Solution , will never forget this solution .
@vikasbagri1225
@vikasbagri1225 Жыл бұрын
I do not know who has observed the optimal approach but whoever the guy was, it was brilliant Thanks for your explanation Striver Understood it very well And waiting for your Binary Search series...
@belugaop7784
@belugaop7784 7 ай бұрын
i generally dont comment on videos , but yaar striver truly is a genius
@calvinseptyanto3110
@calvinseptyanto3110 10 ай бұрын
Had to give this a like, can't lie the best explanation for this problem.
@moviesbuddy4228
@moviesbuddy4228 3 ай бұрын
class Solution { public: int maxProduct(vector& nums) { double prefix = 1, suffix = 1 , ans = INT_MIN; int n = nums.size(); for(int i=0;i
@shubhamagarwal1434
@shubhamagarwal1434 3 ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.....
@CrytpoDeFi308
@CrytpoDeFi308 Жыл бұрын
Was struggling to understand the explanation of this problem, but after tour video I am amazed how easy it is :D
@najimali32
@najimali32 6 ай бұрын
This approach is awesome! I've seen others teaching the same old Kadane approach, but this observation-based method is a real gem. Keep up the great work and keep sharing your insights!
@AJK-a2j00k4
@AJK-a2j00k4 7 ай бұрын
The way he did it in one go without taking separate arrays before and after 0 like I did.. hatsoff you are truly amazing!
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn Ай бұрын
THE GOAT of the DSA prep 🔥 hands down 🙌
@CinematicClips-uz3mk
@CinematicClips-uz3mk 6 ай бұрын
Completed this playlist.... Gain confidence and knowledge.... Thank you Bhaiya ❤❤
@Manishgupta200
@Manishgupta200 Жыл бұрын
The logical thinking It's very easy to understand.. with TC -> O(N) and SC -> O(1)
@Anything-cx4bs
@Anything-cx4bs 5 ай бұрын
you are really the best teacher available on youtube
@madhulikaghosh3876
@madhulikaghosh3876 8 ай бұрын
Happy to see your video. So many videos I watched for this question. Finally i can get the intutive for the question.
@councilaxe
@councilaxe Жыл бұрын
The ans=max() part should come before we set the prefix and suffix as 1 Otherwise say in cases like [-ve ,0,-ve] the answer will be 1 instead of 0
@Shanky_17
@Shanky_17 Жыл бұрын
Nice obs..
@adityagoswami6881
@adityagoswami6881 Жыл бұрын
the solution was giving error for leetcode test case, so i put the finding max part before if condition , the solution got accepted
@Ali-kf6jk
@Ali-kf6jk Жыл бұрын
It should work if at the start you intitialize the max at min_int or min infinity in python. This way 0 will get picked. No need to change the max() part.
@shivanshuranjan-iitbhu889
@shivanshuranjan-iitbhu889 Жыл бұрын
Another thing you can do is to store the maximum element of array in a maxele variable and return max(maxele,ans)
@anshusoni6261
@anshusoni6261 5 ай бұрын
But the solution is giving runtime error on Leetcode
@saunaknandi1814
@saunaknandi1814 Жыл бұрын
Hey Striver can you add the famous Egg Dropping Problem in your DP series? It will be of great help to many students.
@KishoreKumar-xr3fu
@KishoreKumar-xr3fu 6 ай бұрын
Best explanation ever i have seen for this. Thank you bro 🙏
@tungvuthanh5537
@tungvuthanh5537 Жыл бұрын
I have watched the solution of neetcode and i must admit that you solution is very intuitive 😂
@Brutal-gz3jz
@Brutal-gz3jz 5 ай бұрын
In leetcode take double for suffix,prefix and return value .so the last test case not gonna be int overflow
@sumeetsinghaaryan
@sumeetsinghaaryan 4 ай бұрын
double maxPro = Integer.MIN_VALUE; double pre = 1, suff = 1; int len = nums.length; for(int i=0; i
@shishirkarki9001
@shishirkarki9001 4 ай бұрын
@@sumeetsinghaaryan why is it working for double but shows overflow error when using long?
@VISHALTHAKUR-gg3dg
@VISHALTHAKUR-gg3dg 4 ай бұрын
@@shishirkarki9001 i also didnt get it ?
@shishirkarki9001
@shishirkarki9001 4 ай бұрын
@@VISHALTHAKUR-gg3dg i came to know that double has larger range than long
@sumanpradhan4105
@sumanpradhan4105 4 ай бұрын
thanks broooo
@torishi82
@torishi82 4 ай бұрын
Best explanations and best qualities. You are a striver indeed.
@shivanitayde9177
@shivanitayde9177 3 ай бұрын
bro, you are awesome, no one teaching the DSA like you teach us ..keep going, bro
@Rohit-fz4vd
@Rohit-fz4vd 7 ай бұрын
im so happy that i did this prblm both brute and optimal by myself....only difference in my code was i did traversal two times to calc prefix and suffix😅. BTW THANKS STRIVER YOU ARE GREAT🙇
@iamprateek3220
@iamprateek3220 7 ай бұрын
if you're getting error due to testcases with starting or ending zeroes, then this solution should work: #include #include using namespace std; int subarrayWithMaxProduct(vector &arr){ int n=arr.size(); int pre=1, suff=1,maxprod=INT_MIN,prezero=0,suffzero=0; for(int i=0;i
@christinajoice3838
@christinajoice3838 Ай бұрын
A big shout out to you, well explained, I had the observation to some extent (to the even negative handlings), but i couldnt go till coding my observation completely, this is really well explained. Thanks Striver :)
@cinime
@cinime Жыл бұрын
Understood! Super awesome explanation as always, thank you very very very much for your effort!!
@santanu29
@santanu29 Жыл бұрын
You learn something new everyday ❤. This is the reason I love your videos. Got to learn something new.
@shambhaviaggarwal9977
@shambhaviaggarwal9977 Ай бұрын
This was such a beautiful and elegant approach
@AbhishekKumar-dl6ko
@AbhishekKumar-dl6ko 6 ай бұрын
Completed Arrays on 09/05/24 ..Superb Content and Explanation .....
@stith_pragya
@stith_pragya 7 ай бұрын
UNDERSTOOD.............Thanks a ton............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@stith_pragya
@stith_pragya 7 ай бұрын
Thank You So Much for this wonderful video.......................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@halcyonramirez6469
@halcyonramirez6469 Жыл бұрын
I actually understand this and it makes so much sense. Thanks!
@trailblazer555
@trailblazer555 5 ай бұрын
The way he explained made me to think how to really approach a problem with logical thinking...Great Striver!!!
@GOJOANDSUKUNAFAN
@GOJOANDSUKUNAFAN 5 ай бұрын
How can you help me please
@dhanushsanapala3287
@dhanushsanapala3287 4 ай бұрын
the best approach i have ever seen
@ChiragNanda-k6j
@ChiragNanda-k6j 10 ай бұрын
Thanks for the great playlists for DSA
@thedon713
@thedon713 Жыл бұрын
Hey Brother..At first thank you for your great work..Now I am learning Recursion from your recursion playlist..Love you from Bangladesh
@sudiptamaity-e
@sudiptamaity-e Жыл бұрын
I solved this problem using a extended version of kadane's algorithm: class Solution { public int maxProduct(int[] nums) { int currmin=1,currmax=1,max=nums[0]; for(int n:nums) { int temp=currmax*n; currmax=Math.max(currmax*n,Math.max(currmin*n,n)); currmin=Math.min(temp,Math.min(currmin*n,n)); max=Math.max(max,currmax); } return max; } }
@akshdhingra2700
@akshdhingra2700 5 ай бұрын
This Guy is Next Level
@ashutoshupadhyay5683
@ashutoshupadhyay5683 Жыл бұрын
u just fired it man.....what an awesome explanation....Thanks bro for the help.
@killeraloo3247
@killeraloo3247 Жыл бұрын
Bhai mast video thi. Teeno algo's samajh mein aa gyin.
@anshlyk
@anshlyk Жыл бұрын
Thanks for the great explanation, it feels like you are explaining me one to one the way you place your face on the screen!!
@dank7044
@dank7044 4 ай бұрын
I think the super clean implementation was very nice.
@playwithlinux
@playwithlinux 5 ай бұрын
Nice Explanation and intuition for this problem.. Thanks a million Striver. ♥♥♥
@harshraj3522
@harshraj3522 Жыл бұрын
finally array completed..thanks striver bhaiya
@CyberZeo
@CyberZeo 2 ай бұрын
Thank you so much for this beautiful explanation
@aryasharma69
@aryasharma69 3 ай бұрын
Java code for 191th test case:- class Solution { public int maxProduct(int[] nums) { int n=nums.length; if(n==0)return 0; double ans= Integer.MIN_VALUE; double pref=1, suf=1; for(int i=0; i
@zaid6493
@zaid6493 Жыл бұрын
awesome teaching and please teach every problem how to identify the pattern which algo we need to used.. and try to explain all the approaches. by the way your videos are great you will deserve 1m soon.
@selvaarumugam370
@selvaarumugam370 Жыл бұрын
U jus explained the problem in a way it was made simple
@hi-tk4hu
@hi-tk4hu Жыл бұрын
Yes when I first saw the solution with kadanes it made me depressed fr, but this approach I will never forget
@Irfankhan-nc2gt
@Irfankhan-nc2gt Жыл бұрын
Thankyou Bro❤ I have Huge of respect and Thankfulness for you 🎉you are doing such a great thing ❤❤
@hrithikmulik8480
@hrithikmulik8480 4 ай бұрын
Hey Striver can you add the famous Egg Dropping Problem in your DP series? It will be of great help to many students.
@gauravharbola1334
@gauravharbola1334 Жыл бұрын
Hi striver, first of all this is the best video that i have seen till now for this problem. Secondly, don't you think this is kadanes algorithm only. We did the same thing like adding all the elements till the end and when we got a negative element will turned that to 0. But this time we are doing from both the sides....
@prashanthreddy3840
@prashanthreddy3840 6 ай бұрын
This approach is excellent.
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
amazing explanation just amaze with the approach
@PDSREACTION
@PDSREACTION Жыл бұрын
bro is this array playlist good?
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
@@PDSREACTION Not only array but all the playlists brothers without any doubt
@cruzer_blade.
@cruzer_blade. 9 ай бұрын
best explanation sir 😁😁
@Lord_Biyoma
@Lord_Biyoma 11 ай бұрын
superb and easy to understand explanation !
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
the optimal solution, too good and intuitive as you saidd!!!
@hashcodez757
@hashcodez757 11 күн бұрын
Understood again Bhaiya!!
@bajrangijha
@bajrangijha 7 ай бұрын
mind blowing approach
@jaditi930
@jaditi930 2 ай бұрын
amazing explanation. thanks a lot.
@yogeshedekar6078
@yogeshedekar6078 11 ай бұрын
This solution works well except one edge case where we have a 0 in the array and even number of negative elements. In that case there is a possibility that 0 is our final answer if all other multiplications are negative. A simple tweak can help us solve this just maintain a zeroflag and before returning the answer just check if the answer is negative and the zeroFlag is set in that case we will simply return zero as our answer since we have a zero element in the array and the maximum subarray product excluding zero is negative.
@aarzookhunger9487
@aarzookhunger9487 4 ай бұрын
But that's already covered. Let's take an example as per your expl. : -4 0 -5 , here 0 exists and even negative elements In the first iteration pref=-4 suff = -5 , max=-5 second : pref=0 , suff=0 , max = 0 third : pref=-5 , suff=-4 , max=0 So we need not take any such flag or maybe you can specify any test case where your approach will work.!
@AnujSingh-us5pw
@AnujSingh-us5pw Ай бұрын
​@aarzookhunger9487 bro first of all -4 is greater than -5, and second consider this case [-3,0,1,-2] here we should get 1 we the code will return 0, so the only we need to do is reset the predix and suffix prod to 1 after the max computation
@sujalchandrakar3874
@sujalchandrakar3874 5 ай бұрын
This will pass all testcases in leetcode class Solution { public: int maxProduct(vector& nums) { long long pre=1,suf=1; long long maxi=INT_MIN; for(int i=0;i
@kergeysarjakin5592
@kergeysarjakin5592 5 ай бұрын
thanks bro
@mohammedharoon1167
@mohammedharoon1167 4 ай бұрын
Just another level 🔥
@AbhishekGhume
@AbhishekGhume 5 ай бұрын
One test case fails in leetcode using this solution. Below is the corrected code. Take prefix and suffix as double. class Solution { public int maxProduct(int[] nums) { int max = Integer.MIN_VALUE;; double prefix = 1; double suffix = 1; for(int i=0; i
@bombblaster6945
@bombblaster6945 4 ай бұрын
can please tell me the for taking it as double
@raghav5354
@raghav5354 Жыл бұрын
I am just here to thank you for acknowledging that kadane's algorithm is not intuitive. I thought I was dumb for not being able to come up with kadane's intuition(while every other person I see is like - "oh yes kadane's algorithm, that’s so obvious").
@AdityaSingh-ql9ke
@AdityaSingh-ql9ke Жыл бұрын
After your hinting, I got the approach...but then I realized ,if we have 0s, then what to do
@lokeshroy3944
@lokeshroy3944 Жыл бұрын
optimal approach is very intuitive .......but the one pass implementation is not......but u explained it very well
@vineet1728
@vineet1728 5 ай бұрын
Add checks at line 11 and 12, such that prefix and suffix product does not overflow integer limits. if(prefix
@Lakshya-f4l
@Lakshya-f4l 4 ай бұрын
Giving wrong answer at one test case😬
@sahilrao4592
@sahilrao4592 4 ай бұрын
@@Lakshya-f4l just assign double instead of int .
@graviton001
@graviton001 4 ай бұрын
Test case is 3,0,-2,1 its giving wrong answer
@himanshusingh6229
@himanshusingh6229 5 ай бұрын
Huge respect brother♥
@Ramsiya658
@Ramsiya658 Жыл бұрын
Understood striver much love to you
@HarishSubash
@HarishSubash 3 ай бұрын
double prefix = 1; double suffix = 1; pls consider this logic of double to prevent overflow of int data type in the test case of 190
@tanaysingh5348
@tanaysingh5348 Жыл бұрын
Really needed this
@ujjvalsharma5055
@ujjvalsharma5055 Жыл бұрын
Great Approach!
@dsa_tutorial
@dsa_tutorial 5 ай бұрын
really great explaination !!!
@maheshchaugule8940
@maheshchaugule8940 Ай бұрын
Thank you, very helpful
@Josuke217
@Josuke217 4 ай бұрын
Nice solution
@debanjan10
@debanjan10 Жыл бұрын
Areh Dada ,,, watta approach
@LokeshSharma-hm5jz
@LokeshSharma-hm5jz Жыл бұрын
very nice video, Just like a story. Thanks. great
@pubgbattleground9208
@pubgbattleground9208 Жыл бұрын
ek number solution!!
@rithikgandhi3685
@rithikgandhi3685 7 ай бұрын
Great solution
@hashcodez757
@hashcodez757 5 ай бұрын
Understood Bhaiya!!
@saketjaiswalSJ
@saketjaiswalSJ Жыл бұрын
6:58 optimal
@epistemophilicmetalhead9454
@epistemophilicmetalhead9454 10 ай бұрын
It feels massively illegal to watch these videos for free
@rosepainting8775
@rosepainting8775 Жыл бұрын
I have solved this problem before, but still I would like to know your approach on this
@DeepuPk-g9q
@DeepuPk-g9q 2 ай бұрын
Striver, I think you forget one corner case when input array is [-2,0,-1] this the maximum product of subarray is 0 when you run your code the answer if -1 not zero. ie, The product of left side array of 0 is negative and product of right side of arrya of 0 id negative so maximum product is zero. I think you miss this corner case
@anupamsinghmnnitallahabad6226
@anupamsinghmnnitallahabad6226 Жыл бұрын
what an approach sir
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation and much intuitive
@BabaHanuman2612
@BabaHanuman2612 Жыл бұрын
Which topic will u gonna teach next, Binary search or strings?
@ankit_yadav11
@ankit_yadav11 2 ай бұрын
it failed on -2,0,-1 answer is 0 but this gives -1
@niharikakumar1533
@niharikakumar1533 10 ай бұрын
Thank you bhaiya 🙂
@RakeshVishwakarmaVishwakar-c8i
@RakeshVishwakarmaVishwakar-c8i Жыл бұрын
Agr apke jaise sir paid me bhi comtemt de toh public pakka legi 🫡🫡
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 491 М.
Count Inversions in an Array | Brute and Optimal
24:17
take U forward
Рет қаралды 242 М.
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 185 МЛН
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 341 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
Maximum Product Subarray - Dynamic Programming - Leetcode 152
15:31
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 501 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,9 МЛН
Set Matrix Zeroes | O(1) Space Approach | Brute - Better - Optimal
30:07
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 459 М.
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН