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]
@@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.
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
I offer 1 on 1 tutoring for Data Structures and Analytics! Email me at greg.hogg1@outlook.com - first call is free!
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]
what if one (or more) of them is zero?
I’ve had this in an interview. Your solution fails to account for dividing by zero errors.
@mapledanish3662 can't you just check for 0s, or is that bad?
@@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.
Also was thinking about this solution. Accepted on LeetCode
Computing the product of all elements in O(n), then copying and dividing by the element before pushing is O(n) as well
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
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.
I'll be making long form videos on this stuff soon :)
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
The problem description mentions that division operation is not allowed
Even if one element is zero the product of all elements becomes zero, and the result array is filled with zero, see the problem?
Yeah I didn't have time to add it but you can't use division set out in the problem
Keep going with it!
Thank you, I will!!
Great work❤❤
Thank you so much 😀
How can I learn prefix sums? They confuse me
Why not simply do prod(arr)/arr[i] for each i
What branch of math did you use to solve this? Is this linear algebra?
Just some multiplication.
@GregHogg knowing what list to create in the first place and multiply together I think Is linear algebra
@@mazx19 Once you do linear algebra you'll wish this is what it meant haha
This is just :
Product( Nums ) div Nums[i]
Fails to account for Nums[i] == 0.