Product of array except self | Leetcode

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 167

  • @anshumaan1024
    @anshumaan1024 11 місяців тому +1

    @4:18, code for this approach 🙂🙂
    *TC-> O(N), SC->O(1) but using division operation* ✅✅
    // CODE
    class Solution {
    public:
    vector productExceptSelf(vector& nums) {

    int n = nums.size();
    int p = 1;
    int countZero = 0;
    // multiplying the elements, ignoring zero in the multiplication
    // also counts the number of zeroes
    for(int i=0; i= 2, then all elements in answer will be zero
    if(countZero >= 2){
    return ans;
    }

    for(int i=0; i

  • @cronypau9970
    @cronypau9970 2 роки тому +39

    the cumulative multiplication at 10:41 will be 1 2 6 24

    • @hari7110
      @hari7110 11 місяців тому

      crct bro

  • @rynnxj3935
    @rynnxj3935 3 роки тому +7

    Thanks for you explanation! Although as a beginner I can only roughly understand until 10:00, but I'm gonna save this to my playlist and come back again when I improved myself in the future!

  • @abhid15
    @abhid15 2 роки тому +3

    This is the clearest, most succinct explainer of this problem that I have come across on UA-cam. Kudos.

  • @adhirajmajumder
    @adhirajmajumder 3 роки тому +11

    Most understandable approach on UA-cam ... Appreciate your effort and time

  • @md-ayaz
    @md-ayaz 2 роки тому +3

    Bro, I feel i can solve this question if I have seen this before. First time, it's not possible. I could only come with division solution. Person who can come up with solution is a genius.

  • @sapnokiranii
    @sapnokiranii 4 роки тому +33

    Great explanation, thank you! I cannot come up with such algorithms on my own, I guess I just have to memorize it for interviews.

    • @techdose4u
      @techdose4u  4 роки тому +11

      You will get it soon. For the time being, do it as you find comfortable.

    • @hardikshettigar805
      @hardikshettigar805 3 роки тому +6

      Hey
      How has that turned out for you? Are you placed?

    • @vikasmeena5448
      @vikasmeena5448 Рік тому

      ofc @@hardikshettigar805

  • @shubhamsonal5871
    @shubhamsonal5871 4 роки тому +48

    This question came in goldman sachs exam in 2k19

    • @techdose4u
      @techdose4u  4 роки тому +13

      Wowww.... That means it's important :)

    • @shubhamsonal5871
      @shubhamsonal5871 4 роки тому

      @@techdose4u absolutely.

    • @shubhamsonal5871
      @shubhamsonal5871 4 роки тому

      @@techdose4u please answer my question on one of your videos. Read the first comment and see in the subcomments , my last two comments and approach.
      Here's the link to your video.
      ua-cam.com/video/NWMcj5QFW74/v-deo.html

  • @ashishm8850
    @ashishm8850 3 роки тому +41

    Bhai, solving it in O(1) space was pure genius! It was like watching a thrilling suspense movie. I had solved the mystery to the point that you would you the o/p array in the calculation, but using that extra product variable was genius. Very nice!

    • @techdose4u
      @techdose4u  3 роки тому +4

      😀

    • @ps6846
      @ps6846 3 роки тому +3

      we have taken output array that is extra space . so space complexity is still O(n)

    • @pranavsharma7479
      @pranavsharma7479 2 роки тому

      @@ps6846 bro see ques once then say

  • @shikujha4141
    @shikujha4141 3 роки тому +2

    Very smooth explanation. Due to u I m falling in love of dsa .

  • @dhruvmaindola673
    @dhruvmaindola673 2 роки тому +1

    I did come up with my long solution but the last test case was like a millions of 1s and -1s and the time limit exceeded there. Your approach makes a lot of sense. Just clicking in my head. Nice approach and explanation.

  • @rakshith3547
    @rakshith3547 4 роки тому +4

    Man, finally I understood this problem.. thank you very much

  • @marhawk6468
    @marhawk6468 3 роки тому +4

    This video finally helped me understand the solution. Thank you!

  • @OilUp96
    @OilUp96 Рік тому

    Beautifully explained ,I just needed the missing piece of the puzzle and you just provided me with this video thanks a lot

  • @geekystuffs6209
    @geekystuffs6209 3 роки тому +28

    The last example it should be 24 instead of 12 , the initial o/p array value

    • @ankurprabhu2547
      @ankurprabhu2547 3 роки тому +7

      thank god i was so confused because of this and i knew i was right

    • @dragonite6636
      @dragonite6636 2 роки тому +1

      yes

    • @sufiyaniqbal5280
      @sufiyaniqbal5280 2 роки тому

      thanks ,it tooked 1 hr of me to recognise and finally ur cmt helper

  • @pawanT23
    @pawanT23 2 роки тому

    This is how earning subscribers looks like, great work👏

  • @andersontiban5356
    @andersontiban5356 Рік тому

    Your explanations are clear and easy to understand, thank you!

  • @neeleshkumar873
    @neeleshkumar873 3 роки тому +2

    Nailed it man!!, u made me fall in the problem!!

  • @toshanverma1084
    @toshanverma1084 3 роки тому

    0(1) space complexity approach was simply awesome

  • @merabdu
    @merabdu 2 роки тому +1

    Here is a pure Javascript solution:
    var productExceptSelf = function(nums) {
    let n = nums.length;
    let output = Array(n).fill(1);
    for(let i = 1; i < n; i++) {
    output[i] = output[i - 1] * nums[i - 1];
    }
    let R = 1;
    for (let i = n - 1; i >= 0; i--) {
    output[i] *= R;
    R *= nums[i];
    }
    return output;
    };

  • @anmoljaising
    @anmoljaising 4 роки тому +2

    Awesome!!!! Thank you! I can never think like this!!!

    • @techdose4u
      @techdose4u  4 роки тому

      Welcome. You can with practice :)

  • @ashishm8850
    @ashishm8850 4 роки тому +3

    Excellent, as always! Thank you!

  • @AvinashJ21
    @AvinashJ21 3 роки тому

    Amazing... Thank you much appreciated.. I dint had idea how to solve thiz problem.. 😢😢

  • @simranredij1698
    @simranredij1698 Рік тому

    Amazing explanation. it was so so helpful, thanks a lot!

  • @hari7110
    @hari7110 11 місяців тому

    Thanks bro, understood well😇

  • @paragroy5359
    @paragroy5359 3 роки тому +2

    Pretty nice explanation sir....doing a great job

  • @vijeas4679
    @vijeas4679 2 роки тому

    Love the way u optimised it 🙏🙏
    and also the way u explain thanks.

  • @nayanabhoj3126
    @nayanabhoj3126 4 роки тому +3

    Finally, I understood this problem, Thank you for a good explanation. But test cases without zeros are only passed from this approach, can you guide how to handle with zeros in input?

    • @techdose4u
      @techdose4u  4 роки тому

      I don't remember it now. I will check it.

    • @rohit8021
      @rohit8021 3 роки тому

      It will manage if 0 present

  • @ayushmantiwari6441
    @ayushmantiwari6441 2 роки тому

    just wow explanation

  • @User-ow7rn
    @User-ow7rn 3 роки тому +1

    at 4:26,you said 0 case can be avoided by if else ,what if there are more than 1 zeroes ?can i know what will be the if else condition?

    • @anshumaan1024
      @anshumaan1024 11 місяців тому +1

      if there are more than 1 zero, then all element in the returned array will be zero 🙂🙂, think it yourself
      here is my code have a look ...
      .
      .
      class Solution {
      public:
      vector productExceptSelf(vector& nums) {

      int n = nums.size();
      int p = 1;
      int countZero = 0;
      // multiplying the elements, ignoring zero in the multiplication
      // also counts the number of zeroes
      for(int i=0; i= 2, then all elements in answer will be zero
      if(countZero >= 2){
      return ans;
      }

      for(int i=0; i

  • @serenestrolls-d7b
    @serenestrolls-d7b 2 роки тому

    JS solution -
    const productExceptSelf = function(nums) {
    let result = []
    let product = 1;
    for (let i = 0; i < nums.length; i++) {
    if (i > 0) {
    product *= nums[i-1];
    } else {
    product *= 1;
    }
    result[i] = product;
    }
    product = 1;
    for (let i = nums.length - 1; i >= 0; i--) {
    if (i < nums.length - 1) {
    product *= nums[i+1];
    } else {
    product *= 1;
    }
    result[i] *= product;
    }
    return result;
    };

  • @bostonlights2749
    @bostonlights2749 4 роки тому +3

    This is asked more times than you can imagine

  • @niraj.suryavanshi_
    @niraj.suryavanshi_ Рік тому

    Great analysis 🤯

  • @Cloud-577
    @Cloud-577 3 роки тому +1

    thank you that was very helpful

  • @nawendusingh2858
    @nawendusingh2858 4 роки тому +2

    @10:35 sir a little mistake it should be 24 instead of 12. but that's fine we understood it anyways.

  • @AyushSharma-ng8oq
    @AyushSharma-ng8oq 10 місяців тому

    best explanation

  • @spetsnaz_2
    @spetsnaz_2 4 роки тому +1

    Nice question and approach 👍

  • @genericstospecifics1856
    @genericstospecifics1856 2 роки тому +1

    Like we handled the corner case for 0th index which was final product val, why didn't we handled the last index case explicitly?
    Sorry If I missed something

  • @Ramneet04
    @Ramneet04 Рік тому +1

    Sir so whats the intuition, How should we approach to this question?? The last part is a bit tricky.

  • @krishsingh6122
    @krishsingh6122 Рік тому

    Great explanation!!!

  • @PrashantSingh-pg9vq
    @PrashantSingh-pg9vq 3 роки тому

    ready for interviews...!!

  • @qazaqempire3828
    @qazaqempire3828 Рік тому

    only after i watched it 3 times i start to get it lol. hello from kazakstan! dont do festivals during pandemic like in march 2021!

  • @CHIRANJIBNANDY1
    @CHIRANJIBNANDY1 4 роки тому +1

    Awesome explanation.

  • @rohandevaki4349
    @rohandevaki4349 2 роки тому

    great explaination

  • @stupidbutcurious6206
    @stupidbutcurious6206 2 роки тому

    very well explained... i cam across this question while searching for another similar but a littile more complex... can you please help solve..
    The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i

  • @VaishnaviNigam
    @VaishnaviNigam 3 роки тому +1

    AFTER HITTING MY HEAD AND TRYING TO DO IT MYSELF FOR AROUND 1 HOUR AND THEN WATCHING 3 4 YT VIDEOS........FINALLY I GOT TO UNDERSTAND THE LOGIC FROM UR EXPLAINATION.........THANKS SIR..BUT I REALLY FEAR THAT WHY SUCH PREFIX SUM N SUFFIX SUM APPROACHES DONT CLICK MY HEAD.......... DONT KNOW IF SOMEDAY ITLL HAPPEN OR NOT :( :(

  • @cybersecurity2812
    @cybersecurity2812 3 роки тому

    Another method
    public class Demo {
    public static void main(String[] args) {
    int products[] = {1, 2, 3, 4};
    int total[] = new int[products.length];
    int ct = products.length;
    for (int i = 0; i < ct; i++) {
    int r = 1;
    for (int x = 0; x < ct; x++) {
    if (x == i) {
    continue;
    }
    r *= products[x];
    }
    total[i] = r;
    }
    for (int t = 0; t < ct; t++) {
    System.out.println(total[t]);
    }
    }
    }

  • @shuuuuu659fgg
    @shuuuuu659fgg Рік тому

    class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
    res = []
    prod = 1
    for i in range(len(nums)):
    prod *= nums[i]
    res.append(prod)


    prod = 1
    for i in range(len(nums)-1, -1, -1):
    res[i] = res[i-1] * prod
    prod *= nums[i]
    res[0] = prod
    return res
    nums:[4,3,2,1,2]
    op: [48,16,24,48,24]
    expected:[12,16,24,48,24]

  • @TheSubhro123
    @TheSubhro123 4 роки тому +1

    Very good videos sir... Your videos are very helpful to me because you teach better than my college professors ... By the way sir can you give some advice as to how to get good at programming and what to follow to get good at data structure and algorithms and also programming as well ??

    • @techdose4u
      @techdose4u  4 роки тому

      Yes why not. You can ping me on LinkedIn for your queries.

    • @TheSubhro123
      @TheSubhro123 4 роки тому +1

      Sir how to find you on linked in??

    • @techdose4u
      @techdose4u  4 роки тому

      Link is given on my channel. You can follow that.

    • @TheSubhro123
      @TheSubhro123 4 роки тому +1

      OK sir thank you ...

    • @techdose4u
      @techdose4u  4 роки тому

      Welcome :)

  • @onlyitj
    @onlyitj 3 роки тому +2

    Good explanation; although not sure how it can determine anyone’s success on the job? Not sure what’s the point of asking such questions

    • @techdose4u
      @techdose4u  3 роки тому +1

      Just a way to filter candidates :)

  • @pranshusati5818
    @pranshusati5818 2 роки тому

    class Solution {
    public int[] productExceptSelf(int[] nums) {
    int [] ans =new int[nums.length];
    int product=1;
    for(int i = 0 ;i0; i--){
    ans[i]=ans[i-1]*product;
    product*=nums[i];
    }
    ans[0]=product;
    return ans;
    }
    }

  • @ADNANKHAN-jj2jg
    @ADNANKHAN-jj2jg 3 роки тому

    How would the code be different if instead of "except for self" we would have included that position?

  • @harshhwardhanrai3716
    @harshhwardhanrai3716 3 роки тому +1

    vector productExceptSelf(vector& nums); plz can u explain this line of your code , I know java but I'm having difficulty in understanding this.

    • @techdose4u
      @techdose4u  3 роки тому

      Vector is the return type of function. ProfuctExceptSelf is the function Name and vector inside brackets is receiving the nuns array.

  • @47tejash
    @47tejash 3 роки тому +1

    Could you please post the explanation for integer to English word problem?

  • @shyamprakashm6325
    @shyamprakashm6325 4 роки тому +1

    I have solved already this problem.my solution is simple.
    Step1: find the product of the elements in the array.let say Arr[1,2,3,4].product=4*3*2*1=24
    Product=24.
    Step2: start the loop from 1 to n or 0 to less than n : printf("%d",PRODUCT/arr[i] )
    Output[24/1,24/2,24/3,24/4]
    [24,12,8,6]

    • @f3-faithfitnessfinance
      @f3-faithfitnessfinance 4 роки тому +1

      This won't work..

    • @shyamprakashm6325
      @shyamprakashm6325 4 роки тому

      @@f3-faithfitnessfinance why bro 🙄🙁🙁

    • @f3-faithfitnessfinance
      @f3-faithfitnessfinance 4 роки тому +1

      @@shyamprakashm6325 try submitting this bro
      You will get to know...

    • @babyhl016
      @babyhl016 4 роки тому +1

      This works but the question does not allow division

    • @techdose4u
      @techdose4u  4 роки тому

      The question doesn't allow division method. Even though you use it, you will have to handle many corner cases. Better to do it using multiplication process

  • @cyruspassi457
    @cyruspassi457 4 роки тому +1

    Sir,your way of teaching is excellent,but why your subscribers are so less?

    • @techdose4u
      @techdose4u  4 роки тому +5

      As more and more people get to know me, our community will keep increasing and hence subscribers :)

    • @ashishm8850
      @ashishm8850 4 роки тому

      @@techdose4u Amen to that brother!

  • @sukiratsingh985
    @sukiratsingh985 Рік тому

    but for left cumulative array complexity is n2???So how O(N)

  • @abhishekgupta3698
    @abhishekgupta3698 4 роки тому +1

    How its space complexity is O(1) ?? when output variable is taking N space

    • @10ser9
      @10ser9 3 роки тому

      they tell you that the output is not considered when considering space complexity

  • @harvendrasinghrathore2848
    @harvendrasinghrathore2848 3 роки тому +1

    First find product of all elements
    Then arr[I]=product/arr[i];

  • @maha_349
    @maha_349 3 роки тому +1

    Sir will u please execute the program for the addition of elements except self

  • @seraj_valley
    @seraj_valley 4 роки тому +2

    GOAT

  • @awesomeamazing836
    @awesomeamazing836 3 роки тому

    Please tell me can i use this output array element =(int)(product of all*Math.pow(index element,-1))

  • @sivaganesh4489
    @sivaganesh4489 4 роки тому +1

    Nice

  • @yashgoswami5374
    @yashgoswami5374 4 роки тому +1

    sir I had commented on "last stone weight" video for different approach, please ans

  • @shaikhzaki3358
    @shaikhzaki3358 Рік тому

    I got a question in which one test case is the arr is [1] . What happen

  • @san-gd3dy
    @san-gd3dy 4 роки тому

    why didn't you use int product in 18th line?

  • @TheDailyProphet789
    @TheDailyProphet789 3 роки тому

    Compile ni ho rhaa,, vector mai prblm bta rha haii

  • @pasitopasito1967
    @pasitopasito1967 4 роки тому +1

    Sir jo last me optimised approach aapne bataya usme bhi toh array le rahe ho cumulative store karne ke lia

    • @pasitopasito1967
      @pasitopasito1967 4 роки тому +1

      Phir ye O(1) KAISE HUA???

    • @techdose4u
      @techdose4u  4 роки тому +2

      Because the question has mentioned that output array is not counted as extra space. Thats why.

    • @pasitopasito1967
      @pasitopasito1967 4 роки тому +1

      Sir, ds/algo ke jab question nahi hote toh kya hume uska answer dekhna chahiye ya sirf just sochna chahiye jab tak nahi hota ????

    • @techdose4u
      @techdose4u  4 роки тому +3

      Time dedo sochne ke liye. Par boht jyada time bhi nhi dena chahiye wrna time waste hoga.

  • @aviligondagowtham1153
    @aviligondagowtham1153 4 роки тому +1

    Find maximum possible stolen value from houses pls tell this problem as it is an dynamic programming

    • @techdose4u
      @techdose4u  4 роки тому

      What help do you need here?

  • @bholugupta2248
    @bholugupta2248 3 роки тому +1

    Tech Dose

  • @bitrish34
    @bitrish34 4 роки тому +1

    🔥🔥🔥

  • @manishnegi7926
    @manishnegi7926 3 роки тому +3

    vector productExceptSelf(vector& nums, int n) {

    vector ans(n,1);
    long long int p1=1,p2=1;
    for(int i=1;i

  • @pasitopasito1967
    @pasitopasito1967 4 роки тому +1

    Sir,How do you think like that????

    • @techdose4u
      @techdose4u  4 роки тому +1

      It just comes to mind 😅

    • @jaydeepmahajan6598
      @jaydeepmahajan6598 4 роки тому +1

      @@techdose4u we are asking how you approach this problem, what are the things which comes into your mind ?

    • @techdose4u
      @techdose4u  4 роки тому +2

      First I solved by division method. Then I thought about how I can avoid dividing. What all I need to find output[i]. I figured it out and used extra space and again solved it. Then I wanted to do it without extra space and so again had to figure out how to use method 2 inplace. So, the third approach came and did solve it again 😅

    • @jaydeepmahajan6598
      @jaydeepmahajan6598 4 роки тому +1

      @@techdose4u sir ,
      What's your name ? So we can follow you on Instagram or LinkedIn

    • @techdose4u
      @techdose4u  4 роки тому

      I have link for my LinkedLink page on my channel.

  • @staybalancedn
    @staybalancedn 2 роки тому

    This gets complex quick, i only understood the first solution.

  • @MarsTheProgrammer
    @MarsTheProgrammer 2 роки тому

    I have my cs degree and been programming for years, i just cannot for the life of me understand how this works...

  • @bholugupta2248
    @bholugupta2248 3 роки тому

    Sir,,,leetcode ki Minimum divisior within a threshold integer ki solution video bnao.na please

  • @aviligondagowtham1153
    @aviligondagowtham1153 4 роки тому +1

    I have a doubt that u have worked already in product based company..??

  • @godoffortuneful
    @godoffortuneful 2 роки тому

    What if input array contains 0

  • @aryankumar87771
    @aryankumar87771 2 роки тому +1

    sir why have you written Leetcode like Pornhub logo on thumbnail

  • @vasugaur1283
    @vasugaur1283 3 роки тому

    bro plz give solution link to all approaches

  • @ankurgupta4696
    @ankurgupta4696 4 роки тому

    but when nums[i] would be 0 the product would be zero and then the whole output would become 0

  • @rajankhunt7002
    @rajankhunt7002 2 роки тому

    like

  • @Pritamdas-bg7fp
    @Pritamdas-bg7fp 4 роки тому +1

    Sir samaj me nahi aya.. Sir thoda detail me bataaya

    • @techdose4u
      @techdose4u  4 роки тому

      It was in detail only.

    • @Pritamdas-bg7fp
      @Pritamdas-bg7fp 4 роки тому +1

      @@techdose4u sir i think mera basic strong nahi hai... Sir aap hindi me video banaya na..

    • @subham-raj
      @subham-raj 4 роки тому +3

      @@Pritamdas-bg7fp don't be disappointed bro just practice as much as you can. One day these will be easy for you.

    • @techdose4u
      @techdose4u  4 роки тому +2

      Just keep practicing....everyone has the potential inside to be good. You too have it.

    • @Pritamdas-bg7fp
      @Pritamdas-bg7fp 4 роки тому +1

      @@techdose4u thank you sir...

  • @SarveshGupta-bu5ho
    @SarveshGupta-bu5ho 3 роки тому

    Sakhti dekho aapke pehle ek bandi ki video thi chhod ke aaya

  • @Wanderlust1342
    @Wanderlust1342 10 місяців тому

    such a a monotonic voice its giving me brain hemorrhage

  • @ManishGupta-wb2wc
    @ManishGupta-wb2wc 2 роки тому

    very bad explanation.