Maximum Product Subarray | O(N) | Geeks for geeks | GFG | Hindi | Problem Solving | FAANG | Shashwat

Sdílet
Vložit
  • čas přidán 27. 06. 2021
  • Java Plus DSA Complete Placement Course:
    • Java and DSA Course Pl...
    Coding Interview Problem Playlist:
    • Problem Solving | Prod...
    Problem Statement:
    Given an array Arr that contains N integers (may be positive, negative or zero). Find the product of the maximum product subarray.
    Problem Link:
    practice.geeksforgeeks.org/pr...
    Company Tags:
    Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe
    Instagram Handle: (@shashwat_tiwari_st)
    shashwattiwari.page.link/shas...
    #ShashwatTiwari #coding​​ #problemsolving​ #leetcode​ #hackerrank​ #hackerearth​ #codechef​ #codesignal​ #algorithms​ #javaprogramming​ #sde​ #placement #programming

Komentáře • 47

  • @shashwat_tiwari_st
    @shashwat_tiwari_st  Před rokem

    This is a very old video, for better sound and camera quality DSA videos, learn from the below playlist!
    Java Plus DSA ( Java + DSA + Problem Solving )
    czcams.com/play/PLQ7ZAf76c0ZPVdhV1bAjFv0bQc1xHURzE.html

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

    HIDDEN GEM on CZcams! All your solution and explanations are worth the watch!

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

    it fails for -2 0 -1

    • @syno.nymous01
      @syno.nymous01 Před rokem

      You are right, but still answer is accepted by gfg.

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

    *Please Read this*
    In the video, I was showing the output of "max" and "res" in the same variable (I realize it now). But the explanation is no different. We have to look for a maximum of "temp1", "temp2" and "current", as well as their minimum. Please don't confuse yourself. This is the flow:
    max is -3
    min is -18
    res is 6
    max is 180
    min is -10
    res is 180
    max is 0
    min is 0
    res is 180
    max is 2
    min is 0
    res is 180
    180

  • @shubhtiwari4013
    @shubhtiwari4013 Před 5 měsíci

    Keep going

  • @sajan8371
    @sajan8371 Před 2 lety

    Very well explained brother, Keep it up :-)

  • @chinmaykemkar2688
    @chinmaykemkar2688 Před 2 lety

    Thank you so much 4 all your videos

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

    Nice explanation sir

  • @abhilashatiwari4974
    @abhilashatiwari4974 Před 3 lety

    Awesome video!!

  • @kunalkheeva
    @kunalkheeva Před rokem

    Thank you so much!!!

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

    that continue statement is not needed. coz let's take this example : {-2, 0, -1}. you'll get -1 as ans whereas 0 is expected.

  • @haroonahmed9835
    @haroonahmed9835 Před 3 lety

    Loved it

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

    Sir how are you forming this concept like at 8:55 their is no need of -60 but then also you are multiplying it!!

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

      Negative numbers might become positive that's why we are taking Negative numbers. Also I have made this quite back, but somewhere I have done one multiplication error, may be you spotted that so you are correct.
      All you need to know is.
      Current element, min product so far and max product so far, your ans will be one of these.
      Also, I have already made a video on kadanes algorithm , this question I have solved there it hardly takes 5 lines and that code is way more simpler than this.. Link is in the comments.

  • @akankshasinha3352
    @akankshasinha3352 Před 2 lety

    Thank you yaarrrr!!!!

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

    Your code and explanation is different.
    After first iteration, in the video explanation, max value is 6 and min value is -18.,
    But as per code, max value is -3.
    Larger of temp1 and temp2 is compared with current a[i]. Max already set value is not taken into account.
    But in video explanation, it is considered.

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

      In the video, I was showing the output of "max" and "res" in the same variable (I realize it now). But the explanation is no different. We have to look for a maximum of "temp1", "temp2" and "current", as well as their minimum. Please don't confuse yourself. This is the flow:
      max is -3
      min is -18
      res is 6
      max is 180
      min is -10
      res is 180
      max is 0
      min is 0
      res is 180
      max is 2
      min is 0
      res is 180
      180

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

    Good explanation. But you keep jumping from the right side of the board to the left side and then back to right and then back to left ......

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

    you explained well, but there is an issue in your code that at GFG it works. but on the leetcode doesn't pass this test case
    [-2,0,-1]. I test it but it did not work for this test case. can you plz tell me what the problem is in your code? or why it did not work plz check.

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

    Sir I think you miss that case when arr[i]=0 then you are not updating min with 0. You are using continue in if block then min never will be updated with 0.

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

      No, I explained it 0 is acting here as a delimiter, in the code also I have updated min and max with 1 whenever we hit 0.
      This is because we are finding a product here, so if I update min and max with 0, it will be wrong as 0 multiplied with any number will be 0. Therefore, we will update min and max with 1 whenever we see a 0 in the array.

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

      Ok sir

  • @coder_ishwar_kumar
    @coder_ishwar_kumar Před 2 lety

    how -60 is comming min ..it not continuous . i m confused, if in place of 0 there is -5 then it will trnalste to 300 making 300 max ...

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      In the video, I was showing the output of "max" and "res" in the same variable (I realize it now). But the explanation is no different. We have to look for a maximum of "temp1", "temp2" and "current", as well as their minimum. Please don't confuse yourself. This is the flow:
      max is -3
      min is -18
      res is 6
      max is 180
      min is -10
      res is 180
      max is 0
      min is 0
      res is 180
      max is 2
      min is 0
      res is 180
      180

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

    i have a doubt regarding your example that why you take min = -60 that means you multiply arr[0]*arr[2] directly does it form subarray brother. And u already told you that we can use min as well.

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      In the video, I was showing the output of "max" and "res" in the same variable (I realize it now). But the explanation is no different. We have to look for a maximum of "temp1", "temp2" and "current", as well as their minimum. Please don't confuse yourself. This is the flow:
      max is -3
      min is -18
      res is 6
      max is 180
      min is -10
      res is 180
      max is 0
      min is 0
      res is 180
      max is 2
      min is 0
      res is 180
      180

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      See,
      I am not multiplying arr[0] and arr[2].
      it is going in continuation. You can watch the kadanes algorithm video on my channel. There I have explained this question also. You will get all your answers there.

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

    2:47 Yhi tak thaa jo bhi thaa

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

    bro, the logic doesn't work for this test case : [-2,0,-1]

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

      I have updated the solution. Remove the continue statement and it will work fine. Solution is in the video description

    • @samratdas7163
      @samratdas7163 Před 2 lety

      @@shashwat_tiwari_st it works. Thanks!

  • @shashwat_tiwari_st
    @shashwat_tiwari_st  Před 2 lety

    If your solution is not passing all test cases, see below video.
    This question can also be solved optimally using Kadane's algorithm!
    Watch it till the end:
    czcams.com/video/PZN2YkPtUaM/video.html

    • @shourjyajitmaiti2490
      @shourjyajitmaiti2490 Před 2 lety

      Sir,I feel this similar to kadane algo ony this is of max product and that is of max sum...or is there another solution of this problem using kadane algo

  • @ghoshsuman9495
    @ghoshsuman9495 Před 2 lety

    done

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

    GFG IDE is not accepting it

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      hey, I just rechecked and it's working, the solution link is also given in the description, you might have done some typing mistakes, check the solution in the description of this video, let me know if you still face any issues.

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

      I think you dont need to use the continue statement
      for e.g. in test case [-2,0,-1]
      answer should be 0 but min and max gets updated to 1,1 and we move to next index

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      @@abhishekpaswan7821 yup yup, you are right, updated the code. Thanks😃!

  • @kunalhoro8882
    @kunalhoro8882 Před 2 lety

    ummed jaga sakta h apne andar ..lol ...😂😂😂

  • @devanshprakash8354
    @devanshprakash8354 Před 2 lety

    It's kadanes algo

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Před 2 lety

      Not exactly, but yes this problem can be solved using kadanes algorithm, you can check kadanes algorithms video. There I have discussed this problem as well. Code differs a bit.