As of this writing LeetCode added a new test case which cause the above code to fail. The logic is still correct though. The test case fails because of overflow error. With just a small fix, the code still works. Have a look at the updated code on my Github link :)
for those whose code fails at 191 testcase use the below code class Solution { public int maxProduct(int[] nums) { double maxpro=nums[0],right=1,left=1; for(int i=0;i
bhai tum kya bande ho yaar… when other top teachers and youtubers fail to explain the hardest problems, you explain those problems so easily somehow….. Man you deserve so much appreciation… Great work bhai..so much inspired by you… Really top-notch explanations.
Bro ur explanation is awesome!! I am able to think the flow of program and build the intuition easily.. one request bro Plz upload recursion vdos.. Thank you!!
I was really struggling to understand some other solutions for this problem that used dynamic programming, but you really made it simple to understand! Thanks!
answer to ur question is if u observe carefully traversing from left and right indirectly we are calculating product of all sub array and each time storing max subbarray product
Hi, Nikhil! I understand the code and how can you get the answer by using the two comparisons from right and left, but I don't get the logical idea of pointing out that you need to remove the middle number at minute 6:26. Is it just an example of what should we get if you keep with odds numbers instead of an even; or just the idea of having more numbers in general? I don't understand x__x It's not something to will do automatically by following the code?
you don't have to remove the middle number, we are just trying to get the maximum product. 2 negatives will give you a positive number, but 3 negatives will give you a negative number again.
The logic sounds very simple but its difficult to understand for cases where the subarray does not contain the starting (leftmost) or the ending (rightmost) element. When the sub array is in between the array
Can you explain why we need to start from both sides? I'm not able to see any benefit in starting from both sides as both eventually gives the same result.
Code Failed for nums =[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, -10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0] Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range. Below code changes will fix the issue. long leftProduct = 1; long rightProduct = 1; long ans = nums[0]; leftProduct = (leftProduct == 0 || leftProduct < Integer.MIN_VALUE) ? 1 : leftProduct; rightProduct = (rightProduct == 0 || rightProduct < Integer.MIN_VALUE) ? 1 : rightProduct; return (int) ans;
i understand the question and solution everything completely but on submitting it on leetcode after checking it twice it says wrong answer don't know why could you please tell me?
@@nikoo28please check it out class Solution { public int longestConsecutive(int[] nums) { int n = nums.length; int left=1; int right =1; int ans=nums[0]; for(int i=0;i
Hey... can we solve similar kind of question , finding maximum sum of subarray in given array. It usually solved using kadane's algorithm, but can we use this approach? Any way superb explanation ....
Hello, Your videos are so good and the way you explain the question as well as the the solution is very nice please keep making these videos I have one request can you please explain (3. Longest Substring Without Repeating Characters) this question of leetcode
Hi! First of all thank you for the video. I'm having a hard time understanding why we have to calculate the product going from the left and also going from the right side of the array. Why is it that a traversal from start of array to its end does not work? Thank you in advance
@@nikoo28 Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range.
As of this writing LeetCode added a new test case which cause the above code to fail. The logic is still correct though.
The test case fails because of overflow error. With just a small fix, the code still works. Have a look at the updated code on my Github link :)
[0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0] this is the testcase for which it fails; can you tell what the fix is sir?
@@Armaan_Punia_that is what I exactly did in the updated code. Did you have a look?
@@nikoo2805 yesterday I had removed what I thought was redundant, 190/191 test cases then passed, now all did! thank you
@@Armaan_Punia_ bhai iska answer mille toh reply krdena salla do ghanta ho gya h
@@katerghost642 class Solution {
public int maxProduct(int[] nums) {
long leftproduct = 1;
long rightproduct = 1;
long ans = nums[0];
for(int i=0; i
for those whose code fails at 191 testcase use the below code
class Solution {
public int maxProduct(int[] nums) {
double maxpro=nums[0],right=1,left=1;
for(int i=0;i
This is the simplest and most intutive solution.
Very simple and easy to understand. I was following another solution which I found a bit difficult to process.
Thank you, keep up the good work.
Your explanations are so on point yaar... I've been binge-watching all your videos...!
Please make more.
Thank you so much 😀
bhai tum kya bande ho yaar… when other top teachers and youtubers fail to explain the hardest problems, you explain those problems so easily somehow….. Man you deserve so much appreciation… Great work bhai..so much inspired by you… Really top-notch explanations.
that is so kind of you...please share and support as much as you can :)
What an explanation. Really mind-blowing. Saw other videos on the same topic, but this is a really whole different level of logic.
the way you explain things are just Awesome.
Understood this logic but it fails at last testcase of leetcode 152 so please update the logic for the testcase and code too........!!! Thank you !!
since I cannot edit the video, the code has been updated in the github link :)
@@nikoo28 thank you sir !
class Solution {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int maxProduct = nums[0];
int currentMax = nums[0];
int currentMin = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
// Swap currentMax and currentMin when encountering a negative number
int temp = currentMax;
currentMax = currentMin;
currentMin = temp;
}
currentMax = Math.max(nums[i], currentMax * nums[i]);
currentMin = Math.min(nums[i], currentMin * nums[i]);
maxProduct = Math.max(maxProduct, currentMax);
}
return maxProduct;
}
} this is crt logic
Underrated channel.
Fantastic explaination👏👏👏You deserve more likes, more views and definitely more subscribers!!!
Very clean , clear and simple explanation.
Bro ur explanation is awesome!! I am able to think the flow of program and build the intuition easily.. one request bro Plz upload recursion vdos.. Thank you!!
i have made several videos that use recursion. Check out the basics here: th-cam.com/video/FTTHkmnvzlM/w-d-xo.html
Clean , crisp & most underrated !
This is genius. You deserve more likes and more subscribers. I love your systematic way of explaining the reasoning.
Glad you think so!
Crisp and clear explanation. Thank you bro.
I was really struggling to understand some other solutions for this problem that used dynamic programming, but you really made it simple to understand! Thanks!
amazing explanation... Thank you
you did a great job explaining this concept, really helpful!!
Your solution impies that ans subarray lie either in suffix or in prefix in non zero elements are present
simple and exellent explanation
My question is, why does it work?
please elaborate...
😂
answer to ur question is if u observe carefully traversing from left and right indirectly we are calculating product of all sub array and each time storing max subbarray product
AWESOME! God bless u
Thank you! You too!
fantastic solution
I love your explanation!
your explanation is awesome. I totally understand it.
Awesome, thank you!
this is the best simple solution even the editorial solution is nothing infront of this
glad you feel that way!!
OG teacher ❤
I do really like your solution, it is so much easier to understand. Thank you
Hi, Nikhil! I understand the code and how can you get the answer by using the two comparisons from right and left, but I don't get the logical idea of pointing out that you need to remove the middle number at minute 6:26.
Is it just an example of what should we get if you keep with odds numbers instead of an even; or just the idea of having more numbers in general? I don't understand x__x
It's not something to will do automatically by following the code?
you don't have to remove the middle number, we are just trying to get the maximum product. 2 negatives will give you a positive number, but 3 negatives will give you a negative number again.
Great explanation, thanks! I actually solved it in a similar way, but was unsure if it's a way to go since everyone is talking DP nowadays.
Your explanation is superb. Subscribed.
Welcome aboard! 😄
The logic sounds very simple but its difficult to understand for cases where the subarray does not contain the starting (leftmost) or the ending (rightmost) element. When the sub array is in between the array
Excellent explanation. I mean, you made question easy.. valaaaah😊
Glad to hear that
Very good brother. Thank you.
Wonderful solution, thank you
Too concise... Too the point.... Best explanation.... I can compare you with Striver... Though you are better than him at many places.... ❤
Thanks for the view
Great explanation! Thank you
What if all elements are zero. Then it will fail?
Can you explain why we need to start from both sides? I'm not able to see any benefit in starting from both sides as both eventually gives the same result.
I don't understand...did you go though my complete video? It cannot be done if we don't go from both sides.
🎉Great Explaination 😮
Thank you my friend simple and easy
it fails on the last test case on leetcode
since I cannot edit the video, the code has been updated in the github link :)
I LOVE UR CHANEL
Great explanation sir
what if there is no positive element in the array?
Hey actually its such a good explanation. but one mistake in explanation of 2nd testcase(2:10 min) there you don't take the contigous sub-array.
i do take the contiguous sub-array
this code not run in leetcode
op explanation👌
Failed or -1, 0,-3
How ? It passed i got 0
yes, it will pass...and you will get 0
U shud also include bruteforce solutions
wrong hai solution
leetcode per 190 testcase fail ho raha
they have updated the test cases.
thanks for letting me know...I will update the code accordingly
Looks like stiver copied your exact logic:) Thanks for explaining it so clearly !
Thankyou sir ❤
Code Failed for nums =[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, -10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0]
Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range.
Below code changes will fix the issue.
long leftProduct = 1;
long rightProduct = 1;
long ans = nums[0];
leftProduct = (leftProduct == 0 || leftProduct < Integer.MIN_VALUE) ? 1 : leftProduct;
rightProduct = (rightProduct == 0 || rightProduct < Integer.MIN_VALUE) ? 1 : rightProduct;
return (int) ans;
yes, that seems to be a newly added test case. I will update the code accordingly. Thanks for your contribution 😄
i understand the question and solution everything completely but on submitting it on leetcode after checking it twice it says wrong answer don't know why could you please tell me?
Check out the complete code on my github. Link in description. That should give you a hint.
@@nikoo28 i have tried that too but its also not working
@@nikoo28please check it out
class Solution {
public int longestConsecutive(int[] nums) {
int n = nums.length;
int left=1;
int right =1;
int ans=nums[0];
for(int i=0;i
You wont pass 1 test case, for that use double instead of int
i fixed the code in link
Hey... can we solve similar kind of question , finding maximum sum of subarray in given array. It usually solved using kadane's algorithm, but can we use this approach?
Any way superb explanation ....
give me a link to the question.
Hello, Your videos are so good and the way you explain the question as well as the the solution is very nice please keep making these videos I have one request can you please explain (3. Longest Substring Without Repeating Characters) this question of leetcode
i added this problem to my list... :)
best
How are you getting ideas like this?
practice, practice, practice and a lot of practice my friend :)
Adios nikhil
Hi! First of all thank you for the video. I'm having a hard time understanding why we have to calculate the product going from the left and also going from the right side of the array. Why is it that a traversal from start of array to its end does not work?
Thank you in advance
did you follow the explanation of the solution, or just looking at the code?
omg
Hi, awesome explanation but it's failing for test case
nums =[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, -10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0]
output=1981284352 using above logic.
Expected =1000000000 (as per leetcode)
my code passed on leetcode, did you check the one in the video description on github?
@@nikoo28 Your code is also failing on LeetCode. This test case seems to be added recently. Due to overflow the answer is coming wrong. Even though leetcode has said the final answer is within the int range but the intermediate values in calculation are overflowing even the long range.
@@user-ph5ek8tg5l Did you resolve with any approch
What if all elements are zero. Then it will fail?
It won’t fail. The answer will be 0
What if all elements are zero. Then it will fail?
It will not fail