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!
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!
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.
@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
@@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. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-NWMcj5QFW74.html
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.
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?
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
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; };
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
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 :( :(
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 ??
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
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 😅
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]); } } }
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; } }
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]
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