Maximum Product Subarray | Array | Love Babbar DSA Sheet | Amazon🔥

Sdílet
Vložit
  • čas přidán 19. 10. 2021
  • #arrays #coding #programming #competitiveprogramming #coding #dsa
    Hey, Guys in this video I have explained how we can solve the problem 'Maximum Product Subarray '.
    Join our Telegram Channel for more Information
    🔰 Telegram Channel Link = t.me/CodeLibrary1
    🔰 Telegram Discussion Group Link = t.me/CodeLibraryDiscussion
    Subscribe Our 2nd Channel = / @codeupwithtwins6728
    🔰 Array Playlist = • Love Babbar DSA 450 Qu...
    🔰 String Playlist = • Love Babbar DSA 450 Qu...
    🔰 Searching and Sorting Playlist = • Love Babbar DSA 450 Qu...
    🔰 Binary Tree Playlist = • Love Babbar DSA 450 Qu...
    🔰 Linked List Playlist = • Love Babbar DSA 450 Qu...
    🔰 Graph Playlist = • Love Babbar DSA 450 Qu...
    🔰 Dynamic Programming Playlist = • Love Babbar DSA 450 Qu...
    🔰 Informative Videos = • Informative Videos
    🔰 Love Babbar DSA Sheet: drive.google.com/file/d/1FMdN...
    Follow us on Instagram:
    🔰 Shailesh Yogendra: / shaileshyogendra
    🔰 Yogesh Yogendra: / i_am_yogesh_here
    Follow us on LinkedIn:
    🔰 Yogesh Yogendra: / yogesh-yogendra-26bbb518a
    🔰 Shailesh Yogendra: / shailesh-yogendra-8b13...
    Hope you like it. Comment if you have any doubt
    LIKE | SHARE | SUBSCRIBE

Komentáře • 110

  • @shuvbhowmickbestin
    @shuvbhowmickbestin Před rokem +11

    Very well explained Yogesh, please keep uploading content. Don't let the channel die.

  • @The_Inquisitive_Boy
    @The_Inquisitive_Boy Před rokem +2

    Read many solutions on leetcode and watched several YT videos, but finaly got clarity from this video. Thanks man.

  • @gokulnaathb2627
    @gokulnaathb2627 Před 2 lety +3

    This was just so amazing, beautifully explained

  • @sharuk3545
    @sharuk3545 Před 2 lety +1

    bhai mza aa gya iska solution kisi ne b itni ache se explain ni kia h thnk u

  • @satyamkumar6469
    @satyamkumar6469 Před rokem +2

    Thanks dude I was struggling to understand the gfg solution but you made it so easy!

  • @KS0607
    @KS0607 Před rokem

    Thanks....crystal clear....short, crisp & intuitive video...

  • @aryansinha1818
    @aryansinha1818 Před rokem

    Maza aagaya what an awesome explanation. Crisp and to the point. Ty!!!

  • @RahulPatilch
    @RahulPatilch Před 2 lety +2

    Fantastic approach!!

  • @tejasghodkhande4957
    @tejasghodkhande4957 Před 2 lety

    Really, Very Good Explanation...Thank you so much...

  • @neelarya5954
    @neelarya5954 Před 2 lety

    You deserve more subscriber man great work!

  • @shambhavigupta
    @shambhavigupta Před 2 lety +1

    nice explainations, thanks for helping!

  • @leepakshiyadav1643
    @leepakshiyadav1643 Před 2 lety +1

    excellent expalation, thanks :)

  • @niteshkhanna690
    @niteshkhanna690 Před rokem +1

    Ulta sidha batna with confidence 🔥🔥

  • @shaniyadav9117
    @shaniyadav9117 Před rokem

    Very good concept. GeekforGeek ka solution nhi smjh aa rha tha ...👍👍

  • @mdhaidarparwez968
    @mdhaidarparwez968 Před rokem +1

    Bhaiya, please complete the whole dsa sheet.
    Your explanation is great.

  • @_hulk748
    @_hulk748 Před rokem

    Great Explanation Sir ❤✨🙏🙇‍♂

  • @AsliShubh
    @AsliShubh Před 9 měsíci +2

    bro not passing all the test cases in java

  • @siddharthchaudhary2320
    @siddharthchaudhary2320 Před rokem +1

    great explanation 👍👍👍👍,just one question what thinking brought to you in mind that keeping this maximum and minimum is covering all the subarrays like for the first time ??
    Basically I want to know about the intuition to this approach
    !

  • @krishnendughosh2368
    @krishnendughosh2368 Před rokem

    superb explanation buddy.

  • @shantanukumar4081
    @shantanukumar4081 Před 2 lety +1

    Great Explanation ............

  • @joyjain3111
    @joyjain3111 Před rokem +1

    great explanation✨👍🏻

  • @GauravSharma-ze4cu
    @GauravSharma-ze4cu Před rokem +3

    Doesn't understand that what happens when 0 comes into the array??
    Now got it 🙂✌🏻

  • @chandnibhatia1211
    @chandnibhatia1211 Před rokem

    Amazing explanation!!

  • @mohammadkhan5430
    @mohammadkhan5430 Před 2 lety

    Great Explanation. Lots of Love from pakistan

  • @vitaminprotein2217
    @vitaminprotein2217 Před rokem +1

    why cant we apply kadanes algo in it??

  • @LearnWithAnmolll
    @LearnWithAnmolll Před 3 měsíci +1

    😍😍

  • @kunalkheeva
    @kunalkheeva Před rokem

    I was waiting for the explanation of your swap thing to subscribe you.

  • @anupaminvincible9777
    @anupaminvincible9777 Před 2 lety +13

    Basically,kadane's jaisa hi approach hai

  • @codesigner690
    @codesigner690 Před 2 lety

    nice explanation

  • @QGamerWorld
    @QGamerWorld Před 2 lety +1

    which slate you are using for writing?

  • @Hrit
    @Hrit Před 2 lety +9

    Very Well Explanation of code. Really appreciate what you do. But not satisfied with the logic building part.

    • @inclinedscorpio
      @inclinedscorpio Před 2 lety +3

      logic building will come from recursion. If you haven't done the recursion part of this problem then it's high chances that you are going to cram the solution no matter which tutorial you see. So try to first get a recursive soln and then move on to array.

  • @mansijoshi9763
    @mansijoshi9763 Před rokem

    Nice Explanation

  • @mdhaidarparwez968
    @mdhaidarparwez968 Před rokem

    Nice explanation bhaiya

  • @mohammedwaseem8599
    @mohammedwaseem8599 Před 2 lety +1

    Badiya.

  • @gtbaba123
    @gtbaba123 Před rokem

    ty bro

  • @arshdeep011
    @arshdeep011 Před rokem

    Great

  • @Imajay204
    @Imajay204 Před 10 měsíci

    ❤❤

  • @ritikashishodia675
    @ritikashishodia675 Před 2 lety

    Bdia bhai tum kaise soch lete ho hmse to socha hi nahi jata sirf brute force ati h

  • @abhijitbiradar
    @abhijitbiradar Před rokem

    great video!. one request, please talk with slow speed

  • @rockyhandsome7047
    @rockyhandsome7047 Před 2 lety

    Bro what about matrix problm in love babbr not able to find

  • @nagasaikrishna2621
    @nagasaikrishna2621 Před rokem

    great

  • @deependrarathore283
    @deependrarathore283 Před 2 lety

    I wrote the exact code in js , but it's not working , giving wrong ans

  • @Barman1904
    @Barman1904 Před 21 dnem

    Noiceee

  • @gtbaba123
    @gtbaba123 Před rokem

    how to find length of that sub array

  • @03_afshanahmedkhan39
    @03_afshanahmedkhan39 Před rokem

    Bhai aap ne iske liye intuition kaise develop kiya ?

  • @burhanaijaz6218
    @burhanaijaz6218 Před rokem

    hello Sir I have a question,
    what if we want to print the subArray also that is givng us maximum product?
    if anybody knows the answer please reply

    • @sudiptadas2806
      @sudiptadas2806 Před 9 měsíci

      yeah you have to print till the position of the array where you have find the maxsum.

  • @ketangrover4140
    @ketangrover4140 Před 2 lety

    Do we have to write main function in coding test??!

    • @tanisha3613
      @tanisha3613 Před 2 lety +1

      Tht depends on platform. If its already written you ve to write only function. It will be mentioned or u urself can see in code section

  • @pareeksanjay05
    @pareeksanjay05 Před 2 lety +10

    Great thought process, but just want to know how can you think of such solution during interview and you have not heard of such question before?

    • @tanujghatge9287
      @tanujghatge9287 Před 2 lety +4

      I also thought so! How can someone think of such approach in small amount of time!

    • @devchaturvedi3651
      @devchaturvedi3651 Před 2 lety +11

      @@tanujghatge9287 you have to prepare every algorithm possible , be prepared for everything that is already there in books and internet . Practice each and every approach available., because there is no approach that you can discover on the day of interview, approaches and algorithms have already been discovered, you just need to know them.

    • @tanujghatge9287
      @tanujghatge9287 Před 2 lety

      @@devchaturvedi3651 Thanks for the advice brother.

    • @deepaklavate4165
      @deepaklavate4165 Před rokem +2

      i guess by practicing more questions only we can think of such approaches

  • @smartswaggy6114
    @smartswaggy6114 Před 2 lety

    Soln doesnt work for -2,3,-4

  • @puneetgupta2060
    @puneetgupta2060 Před 2 lety

    Ye solution ek bande ne daala tha leetcode par jo University teacher ka hai

  • @soumenpal4927
    @soumenpal4927 Před 10 měsíci

    if 0 come then????????????

  • @chungusvfx5249
    @chungusvfx5249 Před 2 lety

    On gfg , this exact code fails but instead the below mentioned code works and idk what is wrong
    long long ans=arr[0],maximum=arr[0], minimum=arr[0];
    for(int i=1;i

    • @aasthagoyal5059
      @aasthagoyal5059 Před rokem

      How to deal if arr[i]=0??

    • @chungusvfx5249
      @chungusvfx5249 Před rokem

      @@aasthagoyal5059 just like othet cases only. The max and min will suffice for them

  • @sanketmane5838
    @sanketmane5838 Před rokem

    Great explanation brother.

  • @lalit-singh-bisht
    @lalit-singh-bisht Před rokem

    bhai koi aur test case bhi smjhate toh saare doubt clear ho jaata

  • @bibekk
    @bibekk Před rokem

    Optimized soln from -> 3:10

  • @mohammedayan7157
    @mohammedayan7157 Před rokem

    well explained!!!!

  • @tejassrivastava6971
    @tejassrivastava6971 Před rokem +4

    Clear explanation. Thanks !!
    if someone wants AC C++ code:
    class Solution {
    public:
    int maxProduct(vector& nums) {
    // ma mi technique:
    int n = nums.size();
    int ma = nums[0], mi = nums[0], ans = nums[0];
    for(int i=1;i

  • @rishabmalhotra6980
    @rishabmalhotra6980 Před 2 lety +1

    this will not work if there is 0's in between

  • @rohandevaki4349
    @rohandevaki4349 Před 2 lety +6

    if the array has 0 , then both max and min becomes 0 and we lose the previous max right? so what do we do? your code doesnt work for this type of test case:
    1
    5
    6,-3,-10,0,2
    your output =360 , expected output = 180

    • @AR-nw6dv
      @AR-nw6dv Před 2 lety

      Exactly

    • @AMITKUMAR-dj2fv
      @AMITKUMAR-dj2fv Před 2 lety +4

      no output will be 180 only. coz at each step, we are calculating the max of ans and ma and storing them in ans. so at the 3rd index(1-based index), we got 180 which gets stored in ans, after that, we don't require the previous ma because now we have encountered 0. so we have to continue from these steps only.
      May be this code will help you to understand:
      ll maxi = a[0],mini=a[0];
      ll ans=a[0];
      for(ll i=1;i

    • @tejassrivastava6971
      @tejassrivastava6971 Před rokem

      The answer coming is 180 still as we have already stored our ans we dont require previous stuffs.

    • @sriramdinesh8852
      @sriramdinesh8852 Před rokem

      @@AMITKUMAR-dj2fv ans will be 180 but what if the elements are like this:1,2,0,-4,-5
      expected output=20 but using this code we will get 2 which is wrong

    • @Mohit_Yadav18
      @Mohit_Yadav18 Před 16 dny

      @@sriramdinesh8852 Code Is giving 20 as expected becoz -4 will be stored in mini variable first then when it goes on -5 it will do swapping and then product of (-4*-5) will happen.

  • @d1vyanshu
    @d1vyanshu Před 2 lety

    Why are you starting the loop from index 1 and not 0?

    • @tejassrivastava6971
      @tejassrivastava6971 Před rokem

      Coz we have already took mi ma for 0index at starting while decalaring

  • @13manveersingh
    @13manveersingh Před rokem

    test case fails for -2 3 -4

  • @harshitsamdhani2426
    @harshitsamdhani2426 Před rokem

    Thank You please tell intuition as well

  • @leetcode7091
    @leetcode7091 Před 2 lety +5

    What is the intention behind taking mi (why mi, how it helps to find max prod) that was missing in this video? But it was good.

    • @calm8515
      @calm8515 Před 2 lety +7

      buddy, its simple, while including elementa into the sub array and multiplyong them, if you encounter a nagative value, then u will get maximum negative product which is wrong
      example: 2, 3, -4, 1
      max = 2, min =2
      max = 2*3 =6 , max = max(3, 6) = 6
      min = 2*3 =6 min =min(3,6) =3
      max = 6*-4 =-24, max = max(-4, -24) = -24
      min = 3*-4 = -12, min = min(-4, -12) = -4
      which is totally wrong, where max should contain the value of min, where min should contain max value, right
      so this will happen only when we encounter a negative value,
      so need to maintain, min and max
      always and when u encounter a -negative no first swap, min and max, then the flow will be correct

  • @himanshurahi_
    @himanshurahi_ Před 2 lety +2

    3:00 -> solution starts from here.

  • @vanshsamaiya431
    @vanshsamaiya431 Před rokem

    great explaination buddy 🫂

  • @sahilanower9189
    @sahilanower9189 Před 2 lety

    Mera bhi starting mein kaafi saarein wrong answer aaye !! 🤦‍♂️☠

  • @dummyaccount8328
    @dummyaccount8328 Před rokem +2

    Your code doesnt work for this TC
    [-2,3,-4]

  • @lastminutegyan9975
    @lastminutegyan9975 Před rokem

    Bro, bhut fast tha and you were just telling the solution instead of explaining.

  • @chandnibhatia1211
    @chandnibhatia1211 Před rokem

    Can you make a video for count stairs order doesnot matter also

  • @therahul5304
    @therahul5304 Před rokem

    Ugh

  • @yeswanthh5068
    @yeswanthh5068 Před rokem +1

    Explain it in English or change the title to Hindi

  • @bro_code3505
    @bro_code3505 Před rokem

    Optimize it with dp what it is part of

  • @dhillon6449
    @dhillon6449 Před 2 lety

    what if the array got 0 in it?

  • @srjop5566
    @srjop5566 Před 2 lety +2

    bhai this approach is not working when arr[-2,3,-4]
    ans = 24 but by this approach ans = 3

    • @shivanshrajolia3272
      @shivanshrajolia3272 Před 2 lety

      Try applying the approach again. Did you forget to swap? Answer is coming 24 only with the above approach...Here is the maxProduct values that it takes -> -2->-6(after swap) -> 24

  • @prasannapm3220
    @prasannapm3220 Před 2 lety +2

    getting wrong answer with this sol [-2,3,-4]
    class Solution {
    public:
    int maxProduct(vector& nums) {
    int maxi = nums[0];
    int mini = nums[0];
    int ans = nums[0];
    for(int i = 1;i < nums.size();i++){
    if(nums[i] < 0){
    swap(maxi,mini);
    }
    maxi = max(nums[i],maxi*nums[i]);
    mini = max(nums[i],mini*nums[i]);
    ans = max(ans,maxi);
    }
    return ans;

    }
    };

    • @tejassrivastava6971
      @tejassrivastava6971 Před rokem +1

      maxi and mini....in both you're taking max()...take min() in mini.

  • @saifraza4377
    @saifraza4377 Před rokem

    Mereko ye samajh nahi ata ye sb solution bande khud se kaise soch lete h.

    • @lalit-singh-bisht
      @lalit-singh-bisht Před rokem

      shi mein bro....dekh ke bikul nhi laga ki ye approach work kregi...lekin dry run krke sab chal rha hai

  • @targetgateeducation7554

    Khud samajh lo pehla

  • @avikkundu
    @avikkundu Před 2 lety +12

    This code will not run in GFG because of a TestCase: [3 12 15 23 33 -35 30 -40 -30 -30 -30 26 28]
    You need to add another condition there.
    ```
    int maxProduct(vector& arr) {

    int n = arr.size();

    int ans = arr[0];
    int maxi = ans;
    int mini = ans;

    int choice1, choice2;

    for (int i = 1; i < n; i++) {

    choice1 = maxi * arr[i]; // Check Here
    choice2 = mini * arr[i]; // Check Here

    maxi = max(arr[i], max(choice1, choice2)); // Check Here
    mini = min(arr[i], min(choice1, choice2)); // Check Here
    ans = max(ans, maxi);
    }
    return ans;
    }
    ```
    Appreciate ur effort, but try to check your codes in multiple platforms before making video.
    Dont teach incomplete answers.

    • @DebasmitBiswal
      @DebasmitBiswal Před 2 lety +1

      What's the reason for taking choice1 and choice2?

    • @DebasmitBiswal
      @DebasmitBiswal Před 2 lety +1

      Still then the above code doesn't work for the above testcase

    • @AnkitSingh-hh8fx
      @AnkitSingh-hh8fx Před 2 lety +4

      @@DebasmitBiswal actually ye gfg platform ki galti h choice lene ka
      reason ye hai ki arr[i] int hai aur maxi or mini long long h toh
      max(int , long long ) nhi le raha gfg ka compiler

    • @rahulbhardwaj4380
      @rahulbhardwaj4380 Před 2 lety +1

      also the condition for 0 is not desribed
      the code is used is :
      int ans = nums[0];
      int mi = nums[0];
      int ma = nums[0];
      for(int i=1; i 0){
      ma = max(nums[i], nums[i]*ma);
      mi = min(nums[i], mi*nums[i]);
      }
      else if(nums[i] == 0){
      ma = 0;
      mi = 0;
      }
      else{
      swap(ma, mi);
      mi = min(nums[i], mi*nums[i]);
      ma = ma*nums[i];
      }
      ans = max(ma, ans);
      }

  • @rajeshadam978
    @rajeshadam978 Před 2 lety

    nice explanation