Maximum Product Subarray - Best Intuitive Approach Discussed

  Рет қаралды 248,771

take U forward

take U forward

Күн бұрын

Пікірлер: 381
@Iron_Man019
@Iron_Man019 Жыл бұрын
He is a real competitive programmer in all KZbinrs who teaches DSA in India
@lakshsinghania
@lakshsinghania 7 ай бұрын
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 5 ай бұрын
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 4 ай бұрын
Saved me hours of wondering. Thanks mate appreciate it
@codersoham
@codersoham 4 ай бұрын
Thanks a lot bro. I was searching for this for hours until I saw your comment.
@deepk-007
@deepk-007 4 ай бұрын
Bro is the saviour.
@secondthread-uc9bd
@secondthread-uc9bd 4 ай бұрын
works but cheating lol
@brp3522
@brp3522 4 ай бұрын
@@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 9 ай бұрын
Completed this playlist also, MAN this is the best teacher of DSA. UNDERSTOOD!!!!!!!
@sasanklakimsetti8967
@sasanklakimsetti8967 2 ай бұрын
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 4 ай бұрын
How
@SusinSunilkumar
@SusinSunilkumar Жыл бұрын
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...
@messi_codes
@messi_codes Жыл бұрын
Best Solution , will never forget this solution .
@MischievousSoul
@MischievousSoul Жыл бұрын
I was literally searching for the intuitive approach and finally found it.. really appreciate it.. and a great explanation..
@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.
@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...
@calvinseptyanto3110
@calvinseptyanto3110 11 ай бұрын
Had to give this a like, can't lie the best explanation for this problem.
@moviesbuddy4228
@moviesbuddy4228 4 ай бұрын
class Solution { public: int maxProduct(vector& nums) { double prefix = 1, suffix = 1 , ans = INT_MIN; int n = nums.size(); for(int i=0;i
@shreyasingh9847
@shreyasingh9847 Ай бұрын
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.
@belugaop7784
@belugaop7784 9 ай бұрын
i generally dont comment on videos , but yaar striver truly is a genius
@Brutal-gz3jz
@Brutal-gz3jz 6 ай бұрын
In leetcode take double for suffix,prefix and return value .so the last test case not gonna be int overflow
@sumeetsinghaaryan
@sumeetsinghaaryan 5 ай бұрын
double maxPro = Integer.MIN_VALUE; double pre = 1, suff = 1; int len = nums.length; for(int i=0; i
@shishirkarki9001
@shishirkarki9001 5 ай бұрын
@@sumeetsinghaaryan why is it working for double but shows overflow error when using long?
@VISHALTHAKUR-gg3dg
@VISHALTHAKUR-gg3dg 5 ай бұрын
@@shishirkarki9001 i also didnt get it ?
@shishirkarki9001
@shishirkarki9001 5 ай бұрын
@@VISHALTHAKUR-gg3dg i came to know that double has larger range than long
@sumanpradhan4105
@sumanpradhan4105 5 ай бұрын
thanks broooo
@stith_pragya
@stith_pragya 8 ай бұрын
UNDERSTOOD.............Thanks a ton............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@amanpreetsinghbawa1600
@amanpreetsinghbawa1600 15 күн бұрын
🔥Striver fire🔥One of the most mugged up soln turned into an easy observation based soln, Kudos!!!
@madhulikaghosh3876
@madhulikaghosh3876 9 ай бұрын
Happy to see your video. So many videos I watched for this question. Finally i can get the intutive for the question.
@Anything-cx4bs
@Anything-cx4bs 6 ай бұрын
you are really the best teacher available on youtube
@stith_pragya
@stith_pragya 8 ай бұрын
Thank You So Much for this wonderful video.......................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@CryptoDeFi308
@CryptoDeFi308 Жыл бұрын
Was struggling to understand the explanation of this problem, but after tour video I am amazed how easy it is :D
@najimali32
@najimali32 7 ай бұрын
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!
@KishoreKumar-xr3fu
@KishoreKumar-xr3fu 7 ай бұрын
Best explanation ever i have seen for this. Thank you bro 🙏
@CinematicClips-uz3mk
@CinematicClips-uz3mk 8 ай бұрын
Completed this playlist.... Gain confidence and knowledge.... Thank you Bhaiya ❤❤
@torishi82
@torishi82 5 ай бұрын
Best explanations and best qualities. You are a striver indeed.
@shivanitayde9177
@shivanitayde9177 4 ай бұрын
bro, you are awesome, no one teaching the DSA like you teach us ..keep going, bro
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn 2 ай бұрын
THE GOAT of the DSA prep 🔥 hands down 🙌
@christinajoice3838
@christinajoice3838 3 ай бұрын
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 :)
@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.
@sourabhsoni5065
@sourabhsoni5065 18 күн бұрын
Well explained. The intuition part is so good.
@tungvuthanh5537
@tungvuthanh5537 Жыл бұрын
I have watched the solution of neetcode and i must admit that you solution is very intuitive 😂
@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 6 ай бұрын
But the solution is giving runtime error on Leetcode
@shambhaviaggarwal9977
@shambhaviaggarwal9977 2 ай бұрын
This was such a beautiful and elegant approach
@shubhamagarwal1434
@shubhamagarwal1434 4 ай бұрын
#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.....
@hegdevishwa
@hegdevishwa 16 күн бұрын
Genius!! I did prefix and suffix thing but was caught off guard by 0.
@Rohit-fz4vd
@Rohit-fz4vd 8 ай бұрын
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🙇
@halcyonramirez6469
@halcyonramirez6469 Жыл бұрын
I actually understand this and it makes so much sense. Thanks!
@cinime
@cinime Жыл бұрын
Understood! Super awesome explanation as always, thank you very very very much for your effort!!
@killeraloo3247
@killeraloo3247 Жыл бұрын
Bhai mast video thi. Teeno algo's samajh mein aa gyin.
@santanu29
@santanu29 Жыл бұрын
You learn something new everyday ❤. This is the reason I love your videos. Got to learn something new.
@Manishgupta200
@Manishgupta200 Жыл бұрын
The logical thinking It's very easy to understand.. with TC -> O(N) and SC -> O(1)
@dhanushsanapala3287
@dhanushsanapala3287 5 ай бұрын
the best approach i have ever seen
@ChiragNanda-k6j
@ChiragNanda-k6j 11 ай бұрын
Thanks for the great playlists for DSA
@AbhishekKumar-dl6ko
@AbhishekKumar-dl6ko 7 ай бұрын
Completed Arrays on 09/05/24 ..Superb Content and Explanation .....
@cruzer_blade.
@cruzer_blade. 10 ай бұрын
best explanation sir 😁😁
@dank7044
@dank7044 5 ай бұрын
I think the super clean implementation was very nice.
@playwithlinux
@playwithlinux 6 ай бұрын
Nice Explanation and intuition for this problem.. Thanks a million Striver. ♥♥♥
@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!!
@ashutoshupadhyay5683
@ashutoshupadhyay5683 Жыл бұрын
u just fired it man.....what an awesome explanation....Thanks bro for the help.
@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
@AJK-a2j00k4
@AJK-a2j00k4 8 ай бұрын
The way he did it in one go without taking separate arrays before and after 0 like I did.. hatsoff you are truly amazing!
@trailblazer555
@trailblazer555 6 ай бұрын
The way he explained made me to think how to really approach a problem with logical thinking...Great Striver!!!
@GOJOANDSUKUNAFAN
@GOJOANDSUKUNAFAN 6 ай бұрын
How can you help me please
@CyberZeo
@CyberZeo 3 ай бұрын
Thank you so much for this beautiful explanation
@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
@iamprateek3220
@iamprateek3220 8 ай бұрын
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
@akshdhingra2700
@akshdhingra2700 6 ай бұрын
This Guy is Next Level
@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....
@harshraj3522
@harshraj3522 Жыл бұрын
finally array completed..thanks striver bhaiya
@hashcodez757
@hashcodez757 Ай бұрын
Understood again Bhaiya!!
@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
@Lord_Biyoma
@Lord_Biyoma Жыл бұрын
superb and easy to understand explanation !
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
the optimal solution, too good and intuitive as you saidd!!!
@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.
@mohammedharoon1167
@mohammedharoon1167 5 ай бұрын
Just another level 🔥
@hrithikmulik8480
@hrithikmulik8480 6 ай бұрын
Hey Striver can you add the famous Egg Dropping Problem in your DP series? It will be of great help to many students.
@jaditi930
@jaditi930 3 ай бұрын
amazing explanation. thanks a lot.
@himanshusingh6229
@himanshusingh6229 6 ай бұрын
Huge respect brother♥
@Irfankhan-nc2gt
@Irfankhan-nc2gt Жыл бұрын
Thankyou Bro❤ I have Huge of respect and Thankfulness for you 🎉you are doing such a great thing ❤❤
@Ramsiya658
@Ramsiya658 Жыл бұрын
Understood striver much love to you
@prashanthreddy3840
@prashanthreddy3840 7 ай бұрын
This approach is excellent.
@debanjan10
@debanjan10 Жыл бұрын
Areh Dada ,,, watta approach
@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; } }
@aryasharma69
@aryasharma69 4 ай бұрын
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
@bajrangijha
@bajrangijha 8 ай бұрын
mind blowing approach
@ujjvalsharma5055
@ujjvalsharma5055 Жыл бұрын
Great Approach!
@AbhishekGhume
@AbhishekGhume 6 ай бұрын
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 5 ай бұрын
can please tell me the for taking it as double
@tanaysingh5348
@tanaysingh5348 Жыл бұрын
Really needed this
@sujalchandrakar3874
@sujalchandrakar3874 6 ай бұрын
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 6 ай бұрын
thanks bro
@dsa_tutorial
@dsa_tutorial 6 ай бұрын
really great explaination !!!
@YourCodeVerse
@YourCodeVerse Жыл бұрын
Understood✅🔥🔥
@vineet1728
@vineet1728 6 ай бұрын
Add checks at line 11 and 12, such that prefix and suffix product does not overflow integer limits. if(prefix
@Lakshya-f4l
@Lakshya-f4l 5 ай бұрын
Giving wrong answer at one test case😬
@sahilrao4592
@sahilrao4592 5 ай бұрын
@@Lakshya-f4l just assign double instead of int .
@graviton001
@graviton001 5 ай бұрын
Test case is 3,0,-2,1 its giving wrong answer
@LokeshSharma-hm5jz
@LokeshSharma-hm5jz Жыл бұрын
very nice video, Just like a story. Thanks. great
@AbhishekBhattacharjee-j2m
@AbhishekBhattacharjee-j2m Жыл бұрын
UNDERSTOOD 🔥🔥🔥🔥
@pubgbattleground9208
@pubgbattleground9208 Жыл бұрын
ek number solution!!
@maheshchaugule8940
@maheshchaugule8940 2 ай бұрын
Thank you, very helpful
@hashcodez757
@hashcodez757 6 ай бұрын
Understood Bhaiya!!
@kanha12297
@kanha12297 Жыл бұрын
great explanation
@Josuke217
@Josuke217 5 ай бұрын
Nice solution
@yogeshedekar6078
@yogeshedekar6078 Жыл бұрын
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 5 ай бұрын
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 3 ай бұрын
​@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
@samratgupta731
@samratgupta731 Жыл бұрын
Very well explained 👏
@anupamsinghmnnitallahabad6226
@anupamsinghmnnitallahabad6226 Жыл бұрын
what an approach sir
@RakeshVishwakarmaVishwakar-c8i
@RakeshVishwakarmaVishwakar-c8i Жыл бұрын
U r a legend ❤❤
@rithikgandhi3685
@rithikgandhi3685 8 ай бұрын
Great solution
@funcode-tw3cg
@funcode-tw3cg Жыл бұрын
excellent approach !!
@niharikakumar1533
@niharikakumar1533 11 ай бұрын
Thank you bhaiya 🙂
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Good explanation and much intuitive
@shreyas0924
@shreyas0924 10 ай бұрын
This was a good one!!
@JayashreeC-b8k
@JayashreeC-b8k 4 ай бұрын
EXCELLENT
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 521 М.
Count Inversions in an Array | Brute and Optimal
24:17
take U forward
Рет қаралды 257 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 844 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 2 МЛН
Pascal Triangle | Finding nCr in minimal time
26:45
take U forward
Рет қаралды 299 М.
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 1,1 МЛН
Rotate Matrix/Image by 90 Degrees | Brute - Optimal
17:47
take U forward
Рет қаралды 263 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 676 М.
3 Sum | Brute -  Better - Optimal with Codes
38:25
take U forward
Рет қаралды 374 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН