FAANG Interview Question! | Product of Array Except Self - Leetcode 238

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ธ.ค. 2023
  • leetcode, coding interview question, data structures, data structures and algorithms, faang

ความคิดเห็น • 30

  • @GregHogg
    @GregHogg  6 หลายเดือนก่อน

    I offer 1 on 1 tutoring for Data Structures and Analytics! Email me at greg.hogg1@outlook.com - first call is free!

  • @guygamer732
    @guygamer732 7 หลายเดือนก่อน +24

    Better answer: iterate through the array and find the total product of each element. Then go through again and at each index take the total divided by the number at the index…
    [1, 2, 3, 4]
    Total product = 24
    [24/1, 24/2, 24/3, 24/4]
    Which is the same as
    [2*3*4, 1*3*4, 1*2*4, 1*2*3]

    • @raven-888
      @raven-888 7 หลายเดือนก่อน +4

      what if one (or more) of them is zero?

    • @mapledanish3662
      @mapledanish3662 7 หลายเดือนก่อน +4

      I’ve had this in an interview. Your solution fails to account for dividing by zero errors.

    • @ashtinpeaks9972
      @ashtinpeaks9972 7 หลายเดือนก่อน +3

      ​@mapledanish3662 can't you just check for 0s, or is that bad?

    • @YuvalRamot
      @YuvalRamot 6 หลายเดือนก่อน +1

      ​@@mapledanish3662you can just check for zeros, if you have 2 or more, the result should be an array of zeros, if you have 1 the array should be zeros except for the place of the zero, and if no zeros then you divide.
      It's better since it's not using any auxiliary memory.

    • @ivanpaniotov4837
      @ivanpaniotov4837 6 หลายเดือนก่อน

      Also was thinking about this solution. Accepted on LeetCode

  • @AntoineAssierdePompignan
    @AntoineAssierdePompignan 7 หลายเดือนก่อน +6

    Computing the product of all elements in O(n), then copying and dividing by the element before pushing is O(n) as well

  • @mujtabarehman5255
    @mujtabarehman5255 7 หลายเดือนก่อน +1

    This can be done using constant space (aside from the array being returned obviously), but imo the solution is confusing enough as it is lol

  • @jasojone
    @jasojone 7 หลายเดือนก่อน +4

    Do you do this live or have long videos? I love the shorts but I want some deeper explanations when I get to practicing again.

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน +6

      I'll be making long form videos on this stuff soon :)

  • @bharathram8848
    @bharathram8848 7 หลายเดือนก่อน +8

    Is there any reason we couldn’t do it the following way: loop through the original array and record the total product of all elements. Make a new array of same length. Then, in a for-loop, set newArr[i] equal to product / oldArray[i]. Return newArr

    • @nikhileshsankar8076
      @nikhileshsankar8076 7 หลายเดือนก่อน +6

      The problem description mentions that division operation is not allowed

    • @kavinsp
      @kavinsp 7 หลายเดือนก่อน +1

      Even if one element is zero the product of all elements becomes zero, and the result array is filled with zero, see the problem?

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน

      Yeah I didn't have time to add it but you can't use division set out in the problem

  • @maxpavlovsky8163
    @maxpavlovsky8163 7 หลายเดือนก่อน

    Keep going with it!

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน

      Thank you, I will!!

  • @viewer9022
    @viewer9022 7 หลายเดือนก่อน

    Great work❤❤

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน

      Thank you so much 😀

  • @jorgesanabria6484
    @jorgesanabria6484 7 หลายเดือนก่อน

    How can I learn prefix sums? They confuse me

  • @sayanghosh6996
    @sayanghosh6996 7 หลายเดือนก่อน

    Why not simply do prod(arr)/arr[i] for each i

  • @mazx19
    @mazx19 7 หลายเดือนก่อน +1

    What branch of math did you use to solve this? Is this linear algebra?

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน +1

      Just some multiplication.

    • @mazx19
      @mazx19 7 หลายเดือนก่อน

      @GregHogg knowing what list to create in the first place and multiply together I think Is linear algebra

    • @GregHogg
      @GregHogg  7 หลายเดือนก่อน +1

      @@mazx19 Once you do linear algebra you'll wish this is what it meant haha

  • @pmcgee003
    @pmcgee003 7 หลายเดือนก่อน

    This is just :
    Product( Nums ) div Nums[i]

    • @mapledanish3662
      @mapledanish3662 7 หลายเดือนก่อน

      Fails to account for Nums[i] == 0.