I usually don't comment but this kind of video I see my hands automatically goes in comment section for comment. Thanks Nikhil you even demystified how dividing will give results, most of people don't know how dividing is giving the answer and thanks for explain postfix and prefix in detail.
Hi Sir, Eveytime I am searching for a leeetcode problem, I add your name in the suffix hoping you have done a video on it. I have understood each and every video that I have watched. Please do solve all the problems, that will be very helpful for people like me.
I like your way of explaining the problem , with this i learnt to solve problems and as well as the way of explaining the code to others. It helped me a lot in my interview.Thank you so much.
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: # Check if the input list is empty if not nums: return []
length_of_nums = len(nums) # Initialize the result list with 1s result = [1] * length_of_nums product = 1 # Calculate the product of all elements to the left of each element for index in range(length_of_nums): result[index] = product # Store the product of elements to the left product *= nums[index] # Update the product for the next element
product = 1 # Calculate the product of all elements to the right of each element for index in range(length_of_nums - 1, -1, -1): result[index] *= product # Multiply by the product of elements to the right product *= nums[index] # Update the product for the next element return result
C++ most optimized code: class Solution { public: vector productExceptSelf(vector& nums) { int n = nums.size(); vector res(n, 1); int pre = 1; int suf = 1; for(int i=0; i
I used the same approach but instead of having left and right array I create a function that calculates prefix product and another function to calculate suffix product and at the end multiply prefix product and suffix product to obtain final answer but this answer exceeds the time limit. Thank you for simple explanation of same logic from another POV.
Here is my solution in python, i used to for loop and it givs the same answer. arr=[2,1,3,4] arr1=[] r=1 for i in range(len(arr)): pro=arr[i] for j in arr: if j is not pro: r=r*j arr1.append(r) r=1 print(arr1)
Thanks for your feedback. But my friend, my channel primarily focuses on problem solving rather than writing the code. Because the code languages will keep on keep on changing with time. Problem solving skills will always remain the same. I would always encourage you to write the code on your own to become a better developer. If you are looking to learn the code line by line, there are a lot of videos who do a better job than me. :) Plus you have so many AI technologies to explain the code.
Thank you for the video. But somehow I don't know how it's possible to find the solution without knowing this trick (prefix, suffix). What am I missing in my reasoning?
This is a kind pf problem which comes with practice. The idea is that once you solve a problem like this, it will open your mind and you will try to apply the same pattern to future problems.
it gives the following error for the array you created to store right side value. error: cannot find symbol [in __Driver__.java] int[] ret = new Solution().productExceptSelf(param_1); ^ symbol: class Solution location: class __DriverSolution__
bro this problem has a better and more simple solution step 1 - just calculate the product of the whole array in this example the answer would be 1*2*3*4=24 step 2 - in the second step to calculate result [ i ] just divide the calculated result by nums [ i ] e.g to calculate result [ 0 ] = 24/1 =24 result [ 1 ] =24/2 =12 and so on
i think this is good approach then your code please review it solving in O(n) time and o(1) space complexity class Solution { public: vector productExceptSelf(vector& nums) { int zero = 0; int tmul = 1; for(auto num : nums){ if(num) tmul *= num; else{ if(zero){ tmul = 0; break; } zero++; } } for(auto &num : nums){ if(num) num = zero>0 ? 0 : tmul/num; else num = tmul; } return nums; } };
Recently found your channel, whenever I don't understand neetcode solutions I come here. Thank you sir!
@neetcode is also an amazing channel :)
Same!
Damn! You are the best man, there are many channels explaining this problem but you are able to transmit the thought process. Thank you!!!
Next level teaching sir !! You are literally a hidden gem!!
glad you feel that way
I usually don't comment but this kind of video I see my hands automatically goes in comment section for comment.
Thanks Nikhil you even demystified how dividing will give results, most of people don't know how dividing is giving the answer and thanks for explain postfix and prefix in detail.
your explanation is so crisp and clear
The best explanation I found in youtube!
thanks for the lucid explanation , you're a hidden gem :) , you deserve way more subscribers
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
Your explanation is so clear! I like how you draw out everything to explain it! Thank you
The drawing really helps a lot 😄
Hi Sir,
Eveytime I am searching for a leeetcode problem, I add your name in the suffix hoping you have done a video on it. I have understood each and every video that I have watched. Please do solve all the problems, that will be very helpful for people like me.
You have the best videos for leetcode
Thank you so much for explaining each line in a very simple manner.
Here our space complexity can be reduced to O(1)..overall nice explanation
how can be s.c reduced to 0(1);
I like your way of explaining the problem , with this i learnt to solve problems and as well as the way of explaining the code to others.
It helped me a lot in my interview.Thank you so much.
I try to approach the problem in a way as you would do in an interview. Glad it helps :)
like always your explanation and video quality is awesome. i will try to share your videos as much as possible .
So nice of you
Keep going Nikhil, I was stuck and I was about to give up too. I feel taught today
glad that was helpful
THANK YOU , I TRIED VERY HARD BUT COULD NOT SOLVED. YOU CLEARED IT IN A MIN .❤❤❤❤❤❤
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
# Check if the input list is empty
if not nums:
return []
length_of_nums = len(nums)
# Initialize the result list with 1s
result = [1] * length_of_nums
product = 1
# Calculate the product of all elements to the left of each element
for index in range(length_of_nums):
result[index] = product # Store the product of elements to the left
product *= nums[index] # Update the product for the next element
product = 1
# Calculate the product of all elements to the right of each element
for index in range(length_of_nums - 1, -1, -1):
result[index] *= product # Multiply by the product of elements to the right
product *= nums[index] # Update the product for the next element
return result
Man you are god 😭🙏 your explanation is outstanding 👏 you earned a subscriber today ❤️.
C++ most optimized code:
class Solution {
public:
vector productExceptSelf(vector& nums) {
int n = nums.size();
vector res(n, 1);
int pre = 1;
int suf = 1;
for(int i=0; i
I used the same approach but instead of having left and right array I create a function that calculates prefix product and another function to calculate suffix product and at the end multiply prefix product and suffix product to obtain final answer but this answer exceeds the time limit. Thank you for simple explanation of same logic from another POV.
that approach works too
@@nikoo28 Yeahh but for that approach time limit exceeds.
Here is my solution in python, i used to for loop and it givs the same answer.
arr=[2,1,3,4]
arr1=[]
r=1
for i in range(len(arr)):
pro=arr[i]
for j in arr:
if j is not pro:
r=r*j
arr1.append(r)
r=1
print(arr1)
but the time complexity of your solution is O(n^2)...because of the nested loops. You can try to improve that.
@@nikoo28 this will fail when arr = [0,0]
@@avanishraj386 did you try the code given on github? It will work with your sample test case. What error are you getting?
@@nikoo28 I am not talking about your code, I have replied the above comment of "True Coding".
Thanks bhai
leetcode ke videos banate raho mere liye or sabke liye
Love you bro
Thanks for the love
Excellent explanation. I am so glad that I came across your YT channel
i have shared your tutorials to my friends
Thank you so so much 😄
Great explanation, This is the video I am searching for. Thanks
Truly wonderful, Sir! Thank you so much for creating such a helpful video. I really appreciate your effort.
Leetcode has mentioned to try O(1) space solution. You should add one more part in the video for such optimizations.
recently found your videos, and im loving the explanations. thank you !
hope they are of help to you
Very nice explanation, I really search your channel for Ds-algo questions . Thanks sir for teaching free.
Really good explanation. Clearly understood the concept. Thank you!
Fall in love with your explanation ❤❤❤❤❤❤❤❤
The way you teach it feels like there is nothing in DSA
Thanks for the appreciation…I really try to simplify things as much as possible.
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int ans[] = new int[n];
Arrays.fill(ans, 1);
int curr = 1;
for(int i = 0; i < n; i++) {
ans[i] *= curr;
curr *= nums[i];
}
curr = 1;
for(int i = n - 1; i >= 0; i--) {
ans[i] *= curr;
curr *= nums[i];
}
return ans;
}
}
Great explanation..thanks!!
Thank you a lot for such great explanation of the problems!
Wow, that is such a clear soln, thanks!
i was getting time limit exceeded for one of the test cases.
Check out the code given in video description
Sir the question also says You must write an algorithm that runs in O(n) time and without using the division operation.
I don't use the division operator.
God level explanation sir
love the way you teach
Totally understood. Thanks a lot.
but we have to solve this with O(1) space
let me come back to you.
What about the extra space you took for left and right array.
That’s why the space complexity is O(n)
What exactly is your question?
What each line of code does matters and very impotant to understand too. I wish you explain that rather than reading it.
Thanks for your feedback. But my friend, my channel primarily focuses on problem solving rather than writing the code. Because the code languages will keep on keep on changing with time. Problem solving skills will always remain the same. I would always encourage you to write the code on your own to become a better developer.
If you are looking to learn the code line by line, there are a lot of videos who do a better job than me. :)
Plus you have so many AI technologies to explain the code.
Question says no extra space, then why left and right arrays
That part got added to the question recently. It wasn't there when I created the video. Will post an update soon on it.
Great teacher 👏
Sir Best Explanation!!
Good video, very easy to understand.
Very well explained!
Thank you for the video. But somehow I don't know how it's possible to find the solution without knowing this trick (prefix, suffix). What am I missing in my reasoning?
This is a kind pf problem which comes with practice. The idea is that once you solve a problem like this, it will open your mind and you will try to apply the same pattern to future problems.
next level sir
best explanation sir
Next Level Teaching Subscribed !!
Exellent Explaination sir
Thank you so much sir.
Explained very well.
Awesome explanation 🎉
you explained it very well. thanks
So nice of you
Sir i didn't understand the approach that you have explained about 2^32 and 10^12>2^32
What part are you getting confused with?
Nicely explained. Thank you very much
i think this logic only apply for this specific problem ?
it's not generic algo..
Amazing Explaination
Amazing explanation!
Very nice explanation.
so good
Excellent explanation.
Glad you liked it
brackets are not allowed here; to declare an array, place the brackets after the name
int[] left = new int[nums.size()];
~~ ^
[]
this error is coming
Please recheck your code. The declaration is correct. Also check my complete code in the github link available in video description.
@@nikoo28 yes sir I have corrected it ..I haven't made left and right as vectors! Thanks for the solution ! 😄
Thanks a lot....you are great....
u are the best! thx! ❤👏✨
you are the best
Bhaiya why would you used num[i-1] and [n+1] in both the left and right Loop pls explain
please try to look at the explanation of the left and right array once again, we have to skip those elements. if you are still confused..let me know
it gives the following error for the array you created to store right side value.
error: cannot find symbol [in __Driver__.java]
int[] ret = new Solution().productExceptSelf(param_1);
^
symbol: class Solution
location: class __DriverSolution__
Check the github link in the description below, for a working code :)
thank you
Nicest explanation ❤❤
Thank you bro❤❤
watch at 1.25x
thanks
superb
Thanks
you are great :)
amazinggggggg
bro this problem has a better and more simple solution
step 1 - just calculate the product of the whole array in this example the answer would be 1*2*3*4=24
step 2 - in the second step to calculate result [ i ] just divide the calculated result by nums [ i ] e.g to calculate result [ 0 ] = 24/1 =24
result [ 1 ] =24/2 =12 and so on
does this method get accepted? I believe you will hit limits when trying to multiply all the numbers.
Pr o(n) m kha aayi yh
great
i think this is good approach then your code please review it
solving in O(n) time and o(1) space complexity
class Solution {
public:
vector productExceptSelf(vector& nums) {
int zero = 0;
int tmul = 1;
for(auto num : nums){
if(num)
tmul *= num;
else{
if(zero){
tmul = 0;
break;
}
zero++;
}
}
for(auto &num : nums){
if(num)
num = zero>0 ? 0 : tmul/num;
else num = tmul;
}
return nums;
}
};
I am dumb
You r osm 🔥
Thank you so much 😀
Next Level sir
Thank you