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!
@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; }
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 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
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.
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
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; }
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
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 ??
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 :( :(
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: 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]
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; } }
int n = arr.size(); int zeroCount = 0; int mul = 1; for (int num : arr) { if (num == 0) { zeroCount++; } else { mul *= num; } } vector result(n); for (int i = 0; i < n; i++) { if (zeroCount > 1) { result[i] = 0; } else if (zeroCount == 1) { result[i] = (arr[i] == 0) ? mul : 0; } else { result[i] = mul / arr[i]; } } return result;
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 😅
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
the cumulative multiplication at 10:41 will be 1 2 6 24
crct bro
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!
@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
Most understandable approach on UA-cam ... Appreciate your effort and time
Thanks 😊
Great explanation, thank you! I cannot come up with such algorithms on my own, I guess I just have to memorize it for interviews.
You will get it soon. For the time being, do it as you find comfortable.
Hey
How has that turned out for you? Are you placed?
ofc @@hardikshettigar805
This is the clearest, most succinct explainer of this problem that I have come across on UA-cam. Kudos.
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!
😀
we have taken output array that is extra space . so space complexity is still O(n)
@@ps6846 bro see ques once then say
This question came in goldman sachs exam in 2k19
Wowww.... That means it's important :)
@@techdose4u absolutely.
@@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
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.
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.
This video finally helped me understand the solution. Thank you!
Welcome :)
Very smooth explanation. Due to u I m falling in love of dsa .
Man, finally I understood this problem.. thank you very much
Welcome :)
The last example it should be 24 instead of 12 , the initial o/p array value
thank god i was so confused because of this and i knew i was right
yes
thanks ,it tooked 1 hr of me to recognise and finally ur cmt helper
Beautifully explained ,I just needed the missing piece of the puzzle and you just provided me with this video thanks a lot
Nailed it man!!, u made me fall in the problem!!
Great ❤️
Your explanations are clear and easy to understand, thank you!
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?
I don't remember it now. I will check it.
It will manage if 0 present
Awesome!!!! Thank you! I can never think like this!!!
Welcome. You can with practice :)
This is how earning subscribers looks like, great work👏
Thanks bro, understood well😇
Sir so whats the intuition, How should we approach to this question?? The last part is a bit tricky.
Excellent, as always! Thank you!
Welcome :)
Amazing... Thank you much appreciated.. I dint had idea how to solve thiz problem.. 😢😢
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
Amazing explanation. it was so so helpful, thanks a lot!
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?
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
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;
};
Love the way u optimised it 🙏🙏
and also the way u explain thanks.
Great analysis 🤯
Pretty nice explanation sir....doing a great job
Thanks :)
@10:35 sir a little mistake it should be 24 instead of 12. but that's fine we understood it anyways.
Nice :)
Thank your for mentioning this!
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
just wow explanation
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
Just a way to filter candidates :)
Nice question and approach 👍
Thanks dude :)
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;
};
thank you that was very helpful
Welcome 😀
vector productExceptSelf(vector& nums); plz can u explain this line of your code , I know java but I'm having difficulty in understanding this.
Vector is the return type of function. ProfuctExceptSelf is the function Name and vector inside brackets is receiving the nuns array.
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 ??
Yes why not. You can ping me on LinkedIn for your queries.
Sir how to find you on linked in??
Link is given on my channel. You can follow that.
OK sir thank you ...
Welcome :)
0(1) space complexity approach was simply awesome
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 :( :(
It will surely happen someday :)
@@techdose4u HOPING THE SAME 💚
Keep practicing
Great explanation!!!
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]);
}
}
}
How would the code be different if instead of "except for self" we would have included that position?
best explanation
Sir,your way of teaching is excellent,but why your subscribers are so less?
As more and more people get to know me, our community will keep increasing and hence subscribers :)
@@techdose4u Amen to that brother!
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!
Awesome explanation.
Thanks :)
great explaination
Could you please post the explanation for integer to English word problem?
I will try that
How its space complexity is O(1) ?? when output variable is taking N space
they tell you that the output is not considered when considering space complexity
but for left cumulative array complexity is n2???So how O(N)
Sir jo last me optimised approach aapne bataya usme bhi toh array le rahe ho cumulative store karne ke lia
Phir ye O(1) KAISE HUA???
Because the question has mentioned that output array is not counted as extra space. Thats why.
Sir, ds/algo ke jab question nahi hote toh kya hume uska answer dekhna chahiye ya sirf just sochna chahiye jab tak nahi hota ????
Time dedo sochne ke liye. Par boht jyada time bhi nhi dena chahiye wrna time waste hoga.
This is asked more times than you can imagine
Important question!
Please tell me can i use this output array element =(int)(product of all*Math.pow(index element,-1))
Find maximum possible stolen value from houses pls tell this problem as it is an dynamic programming
What help do you need here?
I got a question in which one test case is the arr is [1] . What happen
Sir will u please execute the program for the addition of elements except self
👍🏼
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]
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 a doubt that u have worked already in product based company..??
he works at samsung R&D . query answered
So fast 🤣
ready for interviews...!!
why didn't you use int product in 18th line?
int n = arr.size();
int zeroCount = 0;
int mul = 1;
for (int num : arr) {
if (num == 0) {
zeroCount++;
} else {
mul *= num;
}
}
vector result(n);
for (int i = 0; i < n; i++) {
if (zeroCount > 1) {
result[i] = 0;
} else if (zeroCount == 1) {
result[i] = (arr[i] == 0) ? mul : 0;
} else {
result[i] = mul / arr[i];
}
}
return result;
sir I had commented on "last stone weight" video for different approach, please ans
Okay will see it.
Sir,How do you think like that????
It just comes to mind 😅
@@techdose4u we are asking how you approach this problem, what are the things which comes into your mind ?
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 😅
@@techdose4u sir ,
What's your name ? So we can follow you on Instagram or LinkedIn
I have link for my LinkedLink page on my channel.
Sir,,,leetcode ki Minimum divisior within a threshold integer ki solution video bnao.na please
Compile ni ho rhaa,, vector mai prblm bta rha haii
but when nums[i] would be 0 the product would be zero and then the whole output would become 0
My mistake it works.....
First find product of all elements
Then arr[I]=product/arr[i];
What if input array contains 0
🔥🔥🔥
🔥
I have my cs degree and been programming for years, i just cannot for the life of me understand how this works...
sir why have you written Leetcode like Pornhub logo on thumbnail
GOAT
😁
bro plz give solution link to all approaches
This gets complex quick, i only understood the first solution.
vector productExceptSelf(vector& nums, int n) {
vector ans(n,1);
long long int p1=1,p2=1;
for(int i=1;i
👍🏼
bro can you explain this Approch please
Nice
Thanks :)
Tech Dose
:)
Sir samaj me nahi aya.. Sir thoda detail me bataaya
It was in detail only.
@@techdose4u sir i think mera basic strong nahi hai... Sir aap hindi me video banaya na..
@@Pritamdas-bg7fp don't be disappointed bro just practice as much as you can. One day these will be easy for you.
Just keep practicing....everyone has the potential inside to be good. You too have it.
@@techdose4u thank you sir...
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]
This won't work..
@@f3-faithfitnessfinance why bro 🙄🙁🙁
@@shyamprakashm6325 try submitting this bro
You will get to know...
This works but the question does not allow division
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
like
Sakhti dekho aapke pehle ek bandi ki video thi chhod ke aaya
such a a monotonic voice its giving me brain hemorrhage
very bad explanation.