Это видео недоступно.
Сожалеем об этом.

Maximum Product Subarray - Best Intuitive Approach Discussed

Поделиться
HTML-код
  • Опубликовано: 19 авг 2024

Комментарии • 336

  • @harshgupta5287
    @harshgupta5287 Месяц назад +43

    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 15 дней назад +2

      Saved me hours of wondering. Thanks mate appreciate it

    • @codersoham
      @codersoham 12 дней назад +1

      Thanks a lot bro. I was searching for this for hours until I saw your comment.

    • @deepk-007
      @deepk-007 9 дней назад +2

      Bro is the saviour.

    • @secondthread-uc9bd
      @secondthread-uc9bd 5 дней назад

      works but cheating lol

    • @brp3522
      @brp3522 5 дней назад

      @@secondthread-uc9bd How's this cheating dude??

  • @Iron_Man019
    @Iron_Man019 Год назад +172

    He is a real competitive programmer in all RUclipsrs who teaches DSA in India

    • @lakshsinghania
      @lakshsinghania 3 месяца назад +3

      guys who do cp have better problem solving skills, even doing dsa gives but cp just opens the brain to think in diff solutions

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII Год назад +192

    I had never seen this approach and the way you explain is like you own it.!!!
    Superb work striver bro

  • @Brutal-gz3jz
    @Brutal-gz3jz 2 месяца назад +37

    In leetcode take double for suffix,prefix and return value .so the last test case not gonna be int overflow

    • @sumeetsinghaaryan
      @sumeetsinghaaryan Месяц назад +11

      double maxPro = Integer.MIN_VALUE;
      double pre = 1, suff = 1;
      int len = nums.length;
      for(int i=0; i

    • @shishirkarki9001
      @shishirkarki9001 Месяц назад +1

      @@sumeetsinghaaryan why is it working for double but shows overflow error when using long?

    • @VISHALTHAKUR-gg3dg
      @VISHALTHAKUR-gg3dg Месяц назад

      @@shishirkarki9001 i also didnt get it ?

    • @shishirkarki9001
      @shishirkarki9001 Месяц назад +3

      @@VISHALTHAKUR-gg3dg i came to know that double has larger range than long

    • @sumanpradhan4105
      @sumanpradhan4105 20 дней назад

      thanks broooo

  • @chiragbansod8252
    @chiragbansod8252 5 месяцев назад +8

    Completed this playlist also, MAN this is the best teacher of DSA. UNDERSTOOD!!!!!!!

  • @pramodharshav6091
    @pramodharshav6091 Год назад +28

    You're God sent for us to learn in last moment ❤

  • @MischievousSoul
    @MischievousSoul Год назад +19

    I was literally searching for the intuitive approach and finally found it.. really appreciate it..
    and a great explanation..

  • @AbjSir
    @AbjSir 10 месяцев назад +17

    Arrays completed
    Tomorrow I will revise full arrays before starting binary search

  • @shubhamagarwal1434
    @shubhamagarwal1434 13 дней назад

    #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.....

  • @AJK-a2j00k4
    @AJK-a2j00k4 4 месяца назад +1

    The way he did it in one go without taking separate arrays before and after 0 like I did.. hatsoff you are truly amazing!

  • @SusinSunilkumar
    @SusinSunilkumar 8 месяцев назад +3

    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...

  • @najimali32
    @najimali32 3 месяца назад +3

    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!

  • @trailblazer555
    @trailblazer555 Месяц назад +1

    The way he explained made me to think how to really approach a problem with logical thinking...Great Striver!!!

  • @aryanyadav3926
    @aryanyadav3926 Год назад +11

    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.

  • @Anything-cx4bs
    @Anything-cx4bs 2 месяца назад +2

    you are really the best teacher available on youtube

  • @sujalchandrakar3874
    @sujalchandrakar3874 2 месяца назад +4

    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

  • @messi_codes
    @messi_codes 11 месяцев назад +4

    Best Solution , will never forget this solution .

  • @shivanitayde9177
    @shivanitayde9177 11 дней назад

    bro, you are awesome, no one teaching the DSA like you teach us ..keep going, bro

  • @saunaknandi1814
    @saunaknandi1814 Год назад +43

    Hey Striver can you add the famous Egg Dropping Problem in your DP series? It will be of great help to many students.

  • @calvinseptyanto3110
    @calvinseptyanto3110 7 месяцев назад +5

    Had to give this a like, can't lie the best explanation for this problem.

  • @vikasbagri1225
    @vikasbagri1225 Год назад +9

    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 4 месяца назад +3

    i generally dont comment on videos , but yaar striver truly is a genius

  • @torishi82
    @torishi82 Месяц назад +1

    Best explanations and best qualities. You are a striver indeed.

  • @CrytpoDeFi308
    @CrytpoDeFi308 Год назад +3

    Was struggling to understand the explanation of this problem, but after tour video I am amazed how easy it is :D

  • @CinematicClips-uz3mk
    @CinematicClips-uz3mk 3 месяца назад +2

    Completed this playlist.... Gain confidence and knowledge.... Thank you Bhaiya ❤❤

  • @KishoreKumar-xr3fu
    @KishoreKumar-xr3fu 3 месяца назад +2

    Best explanation ever i have seen for this. Thank you bro 🙏

  • @moviesbuddy4228
    @moviesbuddy4228 16 дней назад

    class Solution {
    public:
    int maxProduct(vector& nums) {
    double prefix = 1, suffix = 1 , ans = INT_MIN;
    int n = nums.size();
    for(int i=0;i

  • @councilaxe
    @councilaxe Год назад +18

    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 11 месяцев назад

      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 10 месяцев назад

      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 10 месяцев назад

      Another thing you can do is to store the maximum element of array in a maxele variable and return max(maxele,ans)

    • @anshusoni6261
      @anshusoni6261 2 месяца назад +3

      But the solution is giving runtime error on Leetcode

  • @aryasharma69
    @aryasharma69 8 дней назад +1

    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

  • @tungvuthanh5537
    @tungvuthanh5537 11 месяцев назад +3

    I have watched the solution of neetcode and i must admit that you solution is very intuitive 😂

  • @madhulikaghosh3876
    @madhulikaghosh3876 4 месяца назад +1

    Happy to see your video. So many videos I watched for this question. Finally i can get the intutive for the question.

  • @thedon713
    @thedon713 Год назад +4

    Hey Brother..At first thank you for your great work..Now I am learning Recursion from your recursion playlist..Love you from Bangladesh

  • @cinime
    @cinime Год назад +4

    Understood! Super awesome explanation as always, thank you very very very much for your effort!!

  • @Rohit-fz4vd
    @Rohit-fz4vd 3 месяца назад

    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🙇

  • @Manishgupta200
    @Manishgupta200 Год назад +1

    The logical thinking It's very easy to understand.. with TC -> O(N) and SC -> O(1)

  • @user-kk9cr4jt4r
    @user-kk9cr4jt4r 9 месяцев назад +2

    Agr apke jaise sir paid me bhi comtemt de toh public pakka legi 🫡🫡

  • @santanu29
    @santanu29 Год назад +1

    You learn something new everyday ❤. This is the reason I love your videos. Got to learn something new.

  • @halcyonramirez6469
    @halcyonramirez6469 Год назад +2

    I actually understand this and it makes so much sense. Thanks!

  • @selvaarumugam370
    @selvaarumugam370 Год назад +2

    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

  • @yogeshedekar6078
    @yogeshedekar6078 8 месяцев назад +2

    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 Месяц назад +1

      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.!

  • @stith_pragya
    @stith_pragya 4 месяца назад +1

    UNDERSTOOD.............Thanks a ton............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @dhanushsanapala3287
    @dhanushsanapala3287 28 дней назад

    the best approach i have ever seen

  • @iamprateek3220
    @iamprateek3220 4 месяца назад

    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

  • @veerendra3911
    @veerendra3911 4 часа назад +1

    i have completed all the arrays but i am forgetting previous questions so give me advice so i can retain all the questions in my mind

  • @user-ru8kq9ci5t
    @user-ru8kq9ci5t 6 месяцев назад +1

    Thanks for the great playlists for DSA

  • @stith_pragya
    @stith_pragya 4 месяца назад +1

    Thank You So Much for this wonderful video.......................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @AbhishekGhume
    @AbhishekGhume Месяц назад +2

    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 Месяц назад

      can please tell me the for taking it as double

  • @zaid6493
    @zaid6493 Год назад +1

    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.

  • @playwithlinux
    @playwithlinux 2 месяца назад

    Nice Explanation and intuition for this problem.. Thanks a million Striver. ♥♥♥

  • @AbhishekKumar-dl6ko
    @AbhishekKumar-dl6ko 3 месяца назад

    Completed Arrays on 09/05/24 ..Superb Content and Explanation .....

  • @anshlyk
    @anshlyk 11 месяцев назад

    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 Месяц назад

    I think the super clean implementation was very nice.

  • @ashutoshupadhyay5683
    @ashutoshupadhyay5683 Год назад

    u just fired it man.....what an awesome explanation....Thanks bro for the help.

  • @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;
    }
    }

  • @user-ng5yf1lo5e
    @user-ng5yf1lo5e 19 дней назад

    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

  • @akshdhingra2700
    @akshdhingra2700 2 месяца назад

    This Guy is Next Level

  • @AdityaSingh-ql9ke
    @AdityaSingh-ql9ke Год назад +3

    After your hinting, I got the approach...but then I realized ,if we have 0s, then what to do

  • @gauravharbola1334
    @gauravharbola1334 Год назад +1

    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....

  • @user-up5of3cm2j
    @user-up5of3cm2j 2 месяца назад

    class Solution {
    public int maxProduct(int[] nums) {
    int pre=1,suf=1,n=nums.length;
    int ans=Integer.MIN_VALUE;
    for(int i=0;i

  • @zyzzbrah9429
    @zyzzbrah9429 Месяц назад +1

    will this interviewer ever be happy bro lol

  • @killeraloo3247
    @killeraloo3247 11 месяцев назад

    Bhai mast video thi. Teeno algo's samajh mein aa gyin.

  • @cruzer_blade.
    @cruzer_blade. 6 месяцев назад +1

    best explanation sir 😁😁

  • @Irfankhan-nc2gt
    @Irfankhan-nc2gt Год назад

    Thankyou Bro❤ I have Huge of respect and Thankfulness for you 🎉you are doing such a great thing ❤❤

  • @RIPNANI
    @RIPNANI 14 дней назад

    Damn bro 🔥 i got it as asaf

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 Год назад +3

    amazing explanation just amaze with the approach

    • @PDSREACTION
      @PDSREACTION Год назад

      bro is this array playlist good?

    • @sukhpreetsingh5200
      @sukhpreetsingh5200 Год назад +3

      @@PDSREACTION Not only array but all the playlists brothers without any doubt

  • @Lord_Biyoma
    @Lord_Biyoma 8 месяцев назад

    superb and easy to understand explanation !

  • @prashanthreddy3840
    @prashanthreddy3840 3 месяца назад

    This approach is excellent.

  • @mohammedharoon1167
    @mohammedharoon1167 Месяц назад

    Just another level 🔥

  • @ishangujarathi10
    @ishangujarathi10 Год назад

    the optimal solution, too good and intuitive as you saidd!!!

  • @arnabmandal1346
    @arnabmandal1346 Месяц назад +1

    Understood

  • @vineet1728
    @vineet1728 Месяц назад

    Add checks at line 11 and 12, such that prefix and suffix product does not overflow integer limits.
    if(prefix

    • @user-ym1nv1pw8i
      @user-ym1nv1pw8i Месяц назад +2

      Giving wrong answer at one test case😬

    • @sahilrao4592
      @sahilrao4592 Месяц назад

      @@user-ym1nv1pw8i just assign double instead of int .

    • @graviton001
      @graviton001 25 дней назад +1

      Test case is 3,0,-2,1 its giving wrong answer

  • @harshraj3522
    @harshraj3522 11 месяцев назад

    finally array completed..thanks striver bhaiya

  • @Josuke217
    @Josuke217 Месяц назад

    Nice solution

  • @lokeshroy3944
    @lokeshroy3944 11 месяцев назад

    optimal approach is very intuitive .......but the one pass implementation is not......but u explained it very well

  • @Ramsiya658
    @Ramsiya658 Год назад +1

    Understood striver much love to you

  • @adityasinghbisht13
    @adityasinghbisht13 Месяц назад

    Leetcode overflow condition
    public int maxProduct(int[] nums) {
    double prefix = 1;
    double suffix = 1;
    int res = nums[0];
    for(int i=0;i

    • @zyzzbrah9429
      @zyzzbrah9429 Месяц назад +1

      explain why on changing long long to double giving the correct answer please explain anyone

  • @akshaykumargupta7297
    @akshaykumargupta7297 Год назад +6

    hey striver what about test case [-2 , 0 , -3] if we convert 0 to 1 and then compute prefix and suffix the answer comes out to be -2 but the actual answer is 0 how to go about that
    if anyone can explain

    • @takeUforward
      @takeUforward  Год назад +9

      Do a try run, you will understand. The multiplication is done, and then the max is updated, and at the next step when you are at -3 the prefix is re-set to 1

    • @akshaykumargupta7297
      @akshaykumargupta7297 Год назад +2

      @@takeUforward okay

  • @epistemophilicmetalhead9454
    @epistemophilicmetalhead9454 7 месяцев назад +1

    It feels massively illegal to watch these videos for free

  • @selene8721
    @selene8721 10 дней назад

    thank you so much!

  • @tanaysingh5348
    @tanaysingh5348 Год назад +1

    Really needed this

  • @bajrangijha
    @bajrangijha 4 месяца назад

    mind blowing approach

  • @dsa_tutorial
    @dsa_tutorial Месяц назад

    really great explaination !!!

  • @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").

  • @JayashreeC-b8k
    @JayashreeC-b8k 7 дней назад

    EXCELLENT

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 4 месяца назад +2

    Array Ends.............

  • @himanshusingh6229
    @himanshusingh6229 2 месяца назад

    Huge respect brother♥

  • @user-jn5pv6oo9d
    @user-jn5pv6oo9d 4 месяца назад

    Some comment i picked up from leetcode discussion to help out with intution :
    There are 2 possibilities - either the number of -ve numbers is even or odd.
    If they are even, then obviously we would want to include all of them(in fact the whole array(unless for zeros)) to maximise the product. This is because multiplying an even number of -ve numbers would make the result +ve.
    If they are odd, then we would want to exclude at most(to maximise the product) one -ve number from our product. So, now the question is, which -ve number to exclude? There are 2 possibilities - first -ve num or last -ve num.
    a. Note that, you cannot exclude a -ve number that is not the first or the last, because, if you do so, you will need to exclude all(because you are breaking the product at this point) other -ve nums following that -ve number and then that needn't result in the maximum product.
    b. Remember, that our goal is to leave out only 1 -ve number so that we can maximise our product.
    c. Note: We are leaving out one -ve number, so that we are able to make the number of -ve nums even. Having said all that, now the question is whether to exclude the first -ve num or the last -ve num in the array. We can only know the answer by trying both.
    d. By taking the product from the beginning of the array, you forcefully include the first -ve number and exclude the last -ve number
    e. vice-versa for taking the product from the end
    To further explain 2d,e, let me take an example:
    Assume the array has an odd number of -ve nums. The first -ve num is -2 and the last -ve num is -3. So the array is .....-2.......-3.......
    The maximum product can either be made of all numbers from the beginning of the array to the first non-zero number just before -3, or from the end of the array to the first non-zero number just after -2.
    This is why we are considering two possible products, one from the beginning and one from the end.
    But wait, you might be thinking, why we are still continuing to multiply even beyond -3(forward iteration) or beyond -2 (backward iteration). That's all actually waste, as the product is only going to increase in negativity beyond those points. The maximum is already updated, so this doesn't affect at all.

  • @dajucoder
    @dajucoder Месяц назад

    152. Maximum Product Subarray
    This solution will stuck in the last test case due to integer constraint. You should put a preventive condition that if the answer >= 10^9 then simply return 10^9. as it is the maximum possible ans

  • @shibainu7500
    @shibainu7500 Месяц назад +1

    NICE

  • @parthsalat
    @parthsalat 2 месяца назад +2

    I though this was about addition

  • @TheITEngineer
    @TheITEngineer 2 месяца назад

    My best DSA programming videos search ended here

  • @rosepainting8775
    @rosepainting8775 Год назад +2

    I have solved this problem before, but still I would like to know your approach on this

  • @naramsettiyedukondalu4182
    @naramsettiyedukondalu4182 24 дня назад

    the output of the array a[] = {-2,0,-1} is 0 . but as per above it returns 1
    @takeUforward
    can u explain it

  • @debanjan10
    @debanjan10 Год назад

    Areh Dada ,,, watta approach

  • @jugaducoder6517
    @jugaducoder6517 11 месяцев назад

    what about the case where zero will be the max product subarr
    eg[-1,0,-2]

  • @ujjvalsharma5055
    @ujjvalsharma5055 10 месяцев назад

    Great Approach!

  • @27Madhav
    @27Madhav 4 дня назад

    Does Arrays ends here or I need to Binary Search Arrays as well to complete whole portion of Arrays?

  • @hemantpatel1413
    @hemantpatel1413 Месяц назад

    Understood.

  • @adityarao4157
    @adityarao4157 Год назад

    after this video i will try to unlearn almost formulas which can be changed by these type of solution with same or less tc
    and try to develop these intuition

  • @viking8774
    @viking8774 Год назад

    One solution for contiguous substring with starting and ending character is same