*corrections* *Initialize the msum to Integer.MIN_VALUE because all the wsum may be negative and in that case the msum will never be updated by the wsum.* and in second method, *maxSum = Math.max(maxSum, windowSum); // added as a part of correction* public static int getMaxSumSubArrayOfSizeKM2(int[] A, int k) { int windowSum = 0, maxSum = Integer.MIN_VALUE; for (int i = 0; i < k; i++) { windowSum += A[i]; } *maxSum = Math.max(maxSum, windowSum); // added as a part of correction* for (int windowEndIndex = k; windowEndIndex < A.length; windowEndIndex++) { windowSum += A[windowEndIndex] - A[windowEndIndex - k]; maxSum = Math.max(maxSum, windowSum); } return maxSum; }
Hi, Actually your code not working for Following input For example int [] a={3,2,6,-1,4,5,-1,2} Exact output will be 11. But your code gives 9. Please change your code little bit following type modle Inside for loop Maxsum=math.max(winsum,maxsum); Winsum+=A[end]-A[end-k]; Now its works fine Its will gives 11
I don't have an interview but was looking for a solution to a sliding window problem for my program, I thank you for your unique style in teaching It's very clear and easy, thank you.
I've been Stuck in this particular problem . Have watched many videos. But I can't understand their way of explanation . But you just Nailed it. Keep doing videos in some special questions asked by huge product based companies please.
Happily subscribed. Great to see you discussing the patterns. Keep them coming. One request though, please site some problems that can be solved using the pattern. It will be an exercise to the viewer and will complete the learning cycle
I ve been finding solution fr this problem over 2 weeks and most of the contant related to this code are neither failed nor not explained correctly but this video is top notch and nailed perfectly.
@@JavaAidTutorials innor prblem ji i have to find index element of maximum value in this same prblm Like 4 5 4 summa = 13 Index = 4 Give me any idea or upload video related to finding index element of subarrray
I can see your efforts in the present it in a more colorful and meaningful way with a nice presentation and communication. awesome job. I see many youtube, but never see one as you presented it very aesthetically. keep up the good work. your subscribers will increase sooner or later,
your videos and explanations are making me feel that Datastructures and algorithms are actually not tough.. I understood this problem so easily ( may be this is an easy one :P).. Thankyou!!
Very nice explanation ...I want more this type of videos.i need to learn new ideas..your videos really superb..please share more videos..with explanation
I think you should assign windows sum to maxsum befor the 2 for loop that's the correct apporch, because I first four is the maxsum them we are getting wrong answer
Let's consider the eg. int[] arr = {2,1,1,-5}; --> our code here will give answer as 2 & now 3 because we are not comparing windowSum found in first loop with the first occurrence of windowSum in the second loop. We have to add: if(maxSum>count) return maxSum; else return count; //copying windowSum value from the first loop in the count PS: I may be wrong, just learning! sorry if I'm :)
Awesome explanation 👍. I got clear clarity. Could you please tell me what is the trick and tips to solve any kind of arrays problems.l mean from easy to complex kind of array problems.
in sliding window technique code it is leaving the first element of the array int [] a={44,45,1,2,3}; then the sum will be 45+1+2+3=51 but it should be 92 because you are not considering the first iteration addition for 0 to k-1 that is 92 uncompared
We have done the correction in our code and mentioned the same in a pinned comment, please follow the pinned comment and let us know if still there is any doubt.
There is a bug in your optimized solution. You are not storing the sum for the initial window in the max sum. So when doing math.max your first value in the array is not included in the computation.
Very nice explanation.I love it Let’s say if the array size is equal to k , the max sum should be sum of array elements. It’s just need a little change for your code, Sir. Thanks a lot
thank you @study student. As this is the corner case and the expectation was to explain the technique so I avoided all validations and corner cases so that it will be easy to grasp the core logic. but I agree with you, to handle this scenario,you need to put an additional check before the second loop and done.
Hello there! I always thought that LeetCode problem #560 could be solved using this technique, but as it turns out I was wrong. Could you please explain Why the Sliding Window Technique does not work for such a case? Thank you, and Great video BTW
I have just reviewed the leetcode problem and found there is a slight difference in both problem statement. they are asking to return "total number of continuous subarrays whose sum is maximum" instead of maximum sum. Here is the leetcode problem statement for your reference. "Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k."
Currently source code is provided only in java language but we are planning to provide it in multiple languages which needs some investment. we will add all feature as we grow. so please share our channel with your friends.It will help us to reach our goal sooner. Mean while you can check out comments on the video ,as lot of people paste their solution in multiple language.
will recommend leetcode and hackerrank. To know more, what to use follow below answer- www.quora.com/Which-one-is-better-HackerRank-or-LeetCode/answer/Kanahaiya-Gupta-2
Arr = [9,6,6,1,2,3,3,5,7,5] K = 3 If maxSum window is the first window(k elements)...then this technique gives a wrong answer It returns 17 instead of 21 To correct it..the Math.Max line should be about the windowSum formula..
There is a error in this problem.... Just write maxsum=windowssum after 1 st for loop then it will work currently.... If u don't believe me... Just put 1st elements of array ie 1 after 4th element of array ie 7 (9,-1,-2,7,1,3,-1,2) u will got output as 10.... Thanks me later
thanks shivam for pointing this out. I have updated my code and mentioned the same in pinned comment. Even it was not necessary but its always good to handle such cases.
There are problems with this code. 1) You are not comparing the first iteration (sum) of the sliding window with maxSum. 2) minimum should be set to Integer.MIN_VALUE. The explanation is great but I think you should have mentioned these corrections in the description box.
For the input {7,8,1,3,1,4,1,0}, the max should be returned as 16, but because of the below code, it is returning as 12. Because for the first iteration, we are not assigning the max. value; the maxSum should be the first stmt in the below loop. for(int j=k;j
If you think this problem is easy and Google always ask hard questions, you can visit this link to clear your doubt. Even the Two sum problem also asked in google interview. th-cam.com/video/XKu_SEDAykw/w-d-xo.html
*corrections*
*Initialize the msum to Integer.MIN_VALUE because all the wsum may be negative and in that case the msum will never be updated by the wsum.*
and
in second method, *maxSum = Math.max(maxSum, windowSum);
// added as a part of correction*
public static int getMaxSumSubArrayOfSizeKM2(int[] A, int k) {
int windowSum = 0, maxSum = Integer.MIN_VALUE;
for (int i = 0; i < k; i++) {
windowSum += A[i];
}
*maxSum = Math.max(maxSum, windowSum);
// added as a part of correction*
for (int windowEndIndex = k; windowEndIndex < A.length; windowEndIndex++) {
windowSum += A[windowEndIndex] - A[windowEndIndex - k];
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}
Hello sir I am yr follower on Hacker rank ,I am also 6 star in Hacker rank but what to do next?
choose another platform like codechef or codeforces and get the 6 star their also.
Hi,
Actually your code not working for
Following input
For example int [] a={3,2,6,-1,4,5,-1,2}
Exact output will be 11.
But your code gives 9.
Please change your code little bit following type modle
Inside for loop
Maxsum=math.max(winsum,maxsum);
Winsum+=A[end]-A[end-k];
Now its works fine
Its will gives 11
@@JavaAidTutorials yes I am doing at code chef there I am 2 star but I want to do job?
Do you have any idea sir?
Did not get your query ? what idea you are talking about?
Explaining tough problems with a dry explanation is easy and commonplace. Your work is brilliant because it makes tough problems seem easy to solve!
Thanks a lot for this feedback.
If you find this channel helpful , please do share among your friends.
I don't have an interview but was looking for a solution to a sliding window problem for my program, I thank you for your unique style in teaching It's very clear and easy, thank you.
I've been Stuck in this particular problem . Have watched many videos. But I can't understand their way of explanation . But you just Nailed it. Keep doing videos in some special questions asked by huge product based companies please.
Glad to hear that
Excellent video and terrific explanation! Best video on this topic on TH-cam!
Thanks for such a lovely feedback.
better understanding after watching the video...!! great going man
trust me this the best video u can find in youtube in this topic
I am sure that your subscribers count will grow with exponential complexity.. Keep the great work up😇
I am completely agree with u
thank you @Aravind
I am also hoping the same. Let's C.
@@himanshujain7014 thanks
After 2 weeks at this, I family got it. You are the best. Thank you so much
Great to hear!
In first for loop you need to write "maxSum =windowSum" because if we are getting the max in our first for loop than we need to return that maxSum;
Happily subscribed. Great to see you discussing the patterns. Keep them coming.
One request though, please site some problems that can be solved using the pattern. It will be an exercise to the viewer and will complete the learning cycle
Thank you for providing such a nice feedback. Sure will start including the problems also.
I ve been finding solution fr this problem over 2 weeks and most of the contant related to this code are neither failed nor not explained correctly but this video is top notch and nailed perfectly.
Thanks for the awesome 🔥 feedback 😊
@@JavaAidTutorials innor prblem ji i have to find index element of maximum value in this same prblm
Like 4 5 4 summa = 13
Index = 4
Give me any idea or upload video related to finding index element of subarrray
Thanks. This is a very common interview question.
yes, it is asked in many coding interview.
Very nice video sir.Thank you so much..
Most welcome
Thank you for explaining this concept in really nice way.
Awesome 👍👍
Most welcome 😊
Your explanation is best than any other youtubers
Thanks for ur explanation sir.
Keep going
From chittoor AP
So nice of you 😀
If you find it useful, please do share with your friends as well. 🙏
I can see your efforts in the present it in a more colorful and meaningful way with a nice presentation and communication. awesome job. I see many youtube, but never see one as you presented it very aesthetically. keep up the good work. your subscribers will increase sooner or later,
Your explanation on this topic was very clear. Keep it up!
you are one of the my friend, from India. Oh my goodness. You are really clear explained in the wsa
Thank you :)
If you like our content please share with others too
Superb bro..... pls continue more on this....
Thank you, We will
Great explanation 🤝
thanks for crystal clear explanation.....
your videos and explanations are making me feel that Datastructures and algorithms are actually not tough.. I understood this problem so easily ( may be this is an easy one :P).. Thankyou!!
You can do it!
Man...your explanations are epic
I have improved my coding skill through this channel.
Glad to hear that.😊
If you find our channel helpful, please support us by sharing it.
I love you man..great explanation.
Great explanation..Just continue making such conceptual videos.Its going to help a lot.
Sure @Subham.
If you find our channel helpful, please do share with your friends too.
Very cool man, keep up the good work.. thanks for adding the correction.. I know many interviewers today look for these edge cases!
Thanks for the feedback 😊,
If you find the tutorial helpful, please do share with your friends also.
Very easy to understand and nice visual representation of sliding window techniq
Thanks vikas for your feedback.
Awesome explanation bro. Keep teaching DSA.
God bless you.
Very nice explanation ...I want more this type of videos.i need to learn new ideas..your videos really superb..please share more videos..with explanation
Thanks for your feedback @arun.
I am trying my best to teach you all.
Stay tuned, for cool stuff.
Thank you for your videos. Respect man! You're Waffentrager E100 in algorithm world!
Thanks 😊
If you find this channel helpful, please support us by sharing it with your friends....🙏🙏
You are an amazing teacher!!!
please more around these please cover all (BS, TEES, DP.....) please -> ur way is easy
Very nice explanation bhaiya... Thank you
You can also use a deque. just add to the end of the list and remove from the front
yes, using dequeue we can solve this problem if size is fixed but it will not work for dynamic size array.
Very nice explanation . Thanks for valuable content ❤️👍 all the best
Thank you so much 🙂
Great sir great great great I will Sher my friend your chanel link thank so mush sir really Good
You are awesome! Thank you very much.
most welcome..:)
Nice explanation !!
Thank you :) @Siva
Very nicely explained. This looks like dynamic programming.
Thanks 😊
Amazing explanation
Glad you think so!
Good work sir keep it.👍
Just awesome ❤️
Great explanation thanks for the wonderful video!
Most welcome 😊
Best explanation!! thanks
GOOD EXPLANATION
Excellent presentation & explanation
Many thanks!
Nice explanation
Thanks @Dheeraj for your feedback.
Super bro👍
thanks :)
Great video sir!
Thanks for watching
It really helped me a lot
thank you. if you find it helpful, please do share with your friends as well.
@@JavaAidTutorials sure Sir
Great explanation.
Nice Approach
Thank you..😊
Awesome explanation! Thanks for putting these up as they are very beneficial.
Glad you like them!
If you find it useful, please do share with other too.
Awesome explanation ! Keep up the good work :)
thanks a lot for your feedback .🙂
Best explanation!
Great explanation with visuals . Subscribed
thanks for your nice feedback..😊
very usefull :)
tq bro.
nicely explained
Thank you 😃
great explanation
Glad you liked it
it's easy to search with question number, so please add question number too in video title.
very helpful!
Glad you think so!
Thank you sir ❤️
I think you should assign windows sum to maxsum befor the 2 for loop that's the correct apporch, because I first four is the maxsum them we are getting wrong answer
you are amazing
Let's consider the eg.
int[] arr = {2,1,1,-5}; --> our code here will give answer as 2 & now 3 because we are not comparing windowSum found in first loop with the first occurrence of windowSum in the second loop.
We have to add:
if(maxSum>count)
return maxSum;
else return count; //copying windowSum value from the first loop in the count
PS: I may be wrong, just learning! sorry if I'm :)
Hi @Nilesh, please have a look on pinnned comment to get the answer for your query.
Awesome explanation 👍. I got clear clarity. Could you please tell me what is the trick and tips to solve any kind of arrays problems.l mean from easy to complex kind of array problems.
Will upload soon, till then keep learning keep coding 😊
@@JavaAidTutorials Thanks a lot 😊
in sliding window technique code it is leaving the first element of the array
int [] a={44,45,1,2,3};
then the sum will be 45+1+2+3=51 but it should be 92
because you are not considering the first iteration addition for 0 to k-1 that is 92 uncompared
between both loop put
maxSum=windowSum
We have done the correction in our code and mentioned the same in a pinned comment, please follow the pinned comment and let us know if still there is any doubt.
Please try to put video based more DSA
Thank you sir
Plss sir also make a vedio on Dutch national flag algorithm technique
Damn Good man
sir can you make video on the largest rectangle&hackerrank solutions on stack game problem
I need some time to upload more content.
Due to lockdown, i am stuck without my laptop 😞
There is a bug in your optimized solution. You are not storing the sum for the initial window in the max sum. So when doing math.max your first value in the array is not included in the computation.
If the (array.length == k), then it will return 0 as maxSum.
So, after first for loop, set
maxSum = windowSum;
Sir ,is prefix sum algorithm a good approach here?
Sir instead of windowsum +=A[end]-A[end-k] we can write windowsum+=A[end]-k
Very nice explanation.I love it
Let’s say if the array size is equal to k , the max sum should be sum of array elements.
It’s just need a little change for your code, Sir.
Thanks a lot
thank you @study student.
As this is the corner case and the expectation was to explain the technique so I avoided all validations and corner cases so that it will be easy to grasp the core logic.
but I agree with you, to handle this scenario,you need to put an additional check before the second loop and done.
Hello there! I always thought that LeetCode problem #560 could be solved using this technique, but as it turns out I was wrong. Could you please explain Why the Sliding Window Technique does not work for such a case? Thank you, and Great video BTW
I have just reviewed the leetcode problem and found there is a slight difference in both problem statement.
they are asking to return "total number of continuous subarrays whose sum is maximum" instead of maximum sum.
Here is the leetcode problem statement for your reference.
"Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k."
Hello sir,please may ypu provide source code in c++ as well
Currently source code is provided only in java language but we are planning to provide it in multiple languages which needs some investment. we will add all feature as we grow. so please share our channel with your friends.It will help us to reach our goal sooner.
Mean while you can check out comments on the video ,as lot of people paste their solution in multiple language.
Can someone please send link to this problem? Leetcode or hackerrank or something else?
Thanks
Welcome
why does prefix sum not work for this problem
please suggest some problems for practicing these algorithms...Codeforces.leetcode,hackerrank any platform..The more the better....
will recommend leetcode and hackerrank.
To know more, what to use follow below answer-
www.quora.com/Which-one-is-better-HackerRank-or-LeetCode/answer/Kanahaiya-Gupta-2
But array is not sorted. Then how can be answer 13 why not(9+7+3+2)=21.
Super
Arr = [9,6,6,1,2,3,3,5,7,5]
K = 3
If maxSum window is the first window(k elements)...then this technique gives a wrong answer
It returns 17 instead of 21
To correct it..the Math.Max line should be about the windowSum formula..
Thanks for pointing this out but i have already mentioned the corrections in my first comment.
Have a look and let me know if i missed something.
Thanks!!!!!!
There is a error in this problem.... Just write maxsum=windowssum after 1 st for loop then it will work currently.... If u don't believe me... Just put 1st elements of array ie 1 after 4th element of array ie 7 (9,-1,-2,7,1,3,-1,2) u will got output as 10.... Thanks me later
hey a small intimation that u are not comparing the first k values with maximum value
yes , we have missed it seems, just added as a part of correction in my pinned comment.
Thanks for pointing it out..:)
Correction: Initialize the msum to Integer.MIN_VALUE because all the wsum may be negative and in that case the msum will never be updated by the wsum.
thanks shivam for pointing this out. I have updated my code and mentioned the same in pinned comment.
Even it was not necessary but its always good to handle such cases.
May I connect you in faceboook Or anywhere else I need help regarding hackerrank questions
sure but don't expect from us to solve hackerrank problems for your assignment or coding interview.. 😀
Tq
There are problems with this code.
1) You are not comparing the first iteration (sum) of the sliding window with maxSum.
2) minimum should be set to Integer.MIN_VALUE.
The explanation is great but I think you should have mentioned these corrections in the description box.
Thanks for your detailed feedback.
we have already added these corrections long back in our pinned comment and git hub code😊.
👌👌
For God sake add subtitles or slow down when you speak
For the input {7,8,1,3,1,4,1,0}, the max should be returned as 16, but because of the below code, it is returning as 12.
Because for the first iteration, we are not assigning the max. value;
the maxSum should be the first stmt in the below loop.
for(int j=k;j
this problem has been already addressed and i have pinged my comment on top. please have a look.
Hello
this cannot be a coding interview problem for google, unless they ask you to do it in DP.. this is basically clickbait
If you think this problem is easy and Google always ask hard questions, you can visit this link to clear your doubt.
Even the Two sum problem also asked in google interview.
th-cam.com/video/XKu_SEDAykw/w-d-xo.html
Don't click this😂
I hope you have not 😜