20:50 algorithm typos mistake =>wsum=wsum-a[i-k]+a[i] instead of "- "you write "+" and one more is in coding that is for first 3 array we can intalize it in "msum" i think u understand but thanks for your awesome explanation really like it
thank you so much for clearing the concept of sliding window . sir but i think the comparison of the sum of the first window is missing in the code. If i am not wrong to fix that logical error we must assign the sum of first window to maximum sum ( Sum = wSum ) just after first for loop.
I have a very simple technique to do this first create your window and then make it slide. Window creating is simple just keep on adding the array elements till i = k - 1 and then slide with this logic For ( i to len-1) Wsum += arr[i] If (i > = k-1) Maxsum = max(maxsum,wsum) Wsum= wsum - arr[ n- (k-1)]
Sir, for the first window that is from 0 to K after calculating sum we should compare it with mSum as it may be the maximum then we proceed with the next window.
Thanks for the effort and clear explanation. I found the code solution might not work if int arr[]={1000,9,31,-4,21,7}; in this case the result should be 1040. Please add the mSum=Math.max(wSum,mSum); after the first for loop. public class SlidingWindow { public static void main(String args[]){ int arr[]={1000,9,31,-4,21,7}; int k=3; System.out.println(findMaxSubArray(arr,k)); } public static int findMaxSubArray(int[] arr, int k){ int wSum=0; int mSum=Integer.MIN_VALUE; for(int i=0;i
you are a man of logic and father of Teaching for me bro i like your Teaching what about this sir? int max= Integer.MIN_VALUE; for (int i=0;imax){ max= arr[i]+arr[i+1]+ arr[i+2]; } } return max; } // O(n) ??
Explanation is good, But there is a minor issue with the code, if you rearrange the array elements this code won't work, you need to do the comparison right after the firstLoop as well for (int i=0; i
if the first sliding window yields the maximum sum then it would return that as a result. Comparison has to also made for the sum of first sliding window.
In naive approach make csum = 0 for every iteration if i. and in inner for loop j must go from i to i+k-1.. also wsum = wsum -a[i-k]+a[i] i runs from i=k to i
You are the best teacher that I found to explain leetcode. There are so many videos but no one could explain it so nicely. Animation and Java code makes it so simple to understand. Thanks so much brother!
at 15:40 when you said remove 9 you actually removed 15 and when you said add 21 you actually added 26 the answer was right 48 but the process was wrong
Video is very informative and the way you explain is wonderful. There is one mistake in program ,if max sum of array is in first window so in that case logic will failed because you couldn't compare first window sum with next window sum. add below line at line no.-10 mSum=wSum;
@TAP ACADEMY I found the bug in the code The optimized code(sliding window) fails if my input array is 10 10 10 5 2 3 and k=3; expected output should be 30 (10+10+10) but the output for above code will be coming out to be 25(10 + 10 +5) this code wont work if the frst k length subarray have max sum so inorder to make it correct after the frst loop you should update the msum i.e msum=Math.max(wsum,msum); try this test case let me know if i m wrong!!
Explain was good but we have not initialize the currentsum , in explaination u r saying that we have to substract the starting of ith element but u r adding there .
your explanation is such a nice one. I am one working profession and need to brush up all these algorithms for Interview Preparation. Please upload more examples of sliding window mechanism.
What if for the first time wsum is greater than all subarrays? It will skip the first hight sum of 0 to 2 array sizes for (int i= k; n > i; i++){ wsum=wsum-a1[i-k]+a1[i]; msum=Math.max(msum,wsum); } Here we are picking up msum after using msum=Math.max(msum,wsum) so that the first 0 to 2 index sum will be skipped if it has greater sum of all subarrays eg first wsum=52 instead of 42 ; can you check it please
i am not getting correct output for naive approach i am getting 113, i am not getting the what issue is. please let me know if anyone found in this code package javaprog; public class subarray_sum_nonNegative_integers { public static void main(String[] args) { // TODO Auto-generated method stub int arr[]= {2,9,31,-4,21,7}; int maxsum=Integer.MIN_VALUE; int sum=0; int k=3;
such a great way of teaching
wow bro i love your style of teaching thank you i understood where will
Superb sir 🎉 just no words 🎉
thank you
Thank u sir 🎉❤❤❤❤
Amazing❤
this my friend is cool.
super sir
Tnx bro :)
👍
we should also check for first wsum may be first one is maximum before sliding
20:50 algorithm typos mistake =>wsum=wsum-a[i-k]+a[i] instead of "- "you write "+" and one more is in coding that is for first 3 array we can intalize it in "msum" i think u understand
but thanks for your awesome explanation really like it
thank you so much for clearing the concept of sliding window . sir but i think the comparison of the sum of the first window is missing in the code. If i am not wrong to fix that logical error we must assign the sum of first window to maximum sum
( Sum = wSum ) just after first for loop.
After Kunal kushwaha this channel is best for learning DSA
Kunal kushwaah teaching well but sometimes he make more hype
Any similar LeetCode Question with this topic??
Sir ,it must be wsum =(wsum-a[i-k] )+a[ i ]
no, it must be wSum= (cSum-a[i-k]+a[i]) at the timeframe you mentioned, at the end while coding, correct formula has been applied by Sir
brilliant ... from editing to teaching
I have a very simple technique to do this first create your window and then make it slide. Window creating is simple just keep on adding the array elements till i = k - 1 and then slide with this logic
For ( i to len-1)
Wsum += arr[i]
If (i > = k-1)
Maxsum = max(maxsum,wsum)
Wsum= wsum - arr[ n- (k-1)]
sir u r explanation is good but there is small mistake in naive approach
int sum=0, maxsum = Integer.min_value;
for(int i=0; i
Can you explain why we add i in the inner loop j
Nobody explained this particular topic better than you. More power to you.
Sir, for the first window that is from 0 to K after calculating sum we should compare it with mSum as it may be the maximum then we proceed with the next window.
sir you are gift for us. thank you so much for these videos
It's my pleasure
Thanks for the effort and clear explanation. I found the code solution might not work if int arr[]={1000,9,31,-4,21,7}; in this case the result should be 1040. Please add the mSum=Math.max(wSum,mSum); after the first for loop.
public class SlidingWindow {
public static void main(String args[]){
int arr[]={1000,9,31,-4,21,7};
int k=3;
System.out.println(findMaxSubArray(arr,k));
}
public static int findMaxSubArray(int[] arr, int k){
int wSum=0;
int mSum=Integer.MIN_VALUE;
for(int i=0;i
you are a man of logic and father of Teaching for me bro i like your Teaching
what about this sir?
int max= Integer.MIN_VALUE;
for (int i=0;imax){
max= arr[i]+arr[i+1]+ arr[i+2];
}
}
return max;
}
// O(n) ??
Really Given knowledge with unique style
Explanation is good, But there is a minor issue with the code, if you rearrange the array elements this code won't work, you need to do the comparison right after the firstLoop as well
for (int i=0; i
if the first sliding window yields the maximum sum then it would return that as a result. Comparison has to also made for the sum of first sliding window.
u look Anirudh Ravichander(Famous Tamil Music director) ;)
This guy is just incredibly talented at explaining stuffs in a much more simpler way. Thanks a lot!
Sliding window also here we are using two for loops. Is not the time complexity O(n2) here?
One of the best explanation on sliding window. Today I understood the concept clearly. Thanks a lot sir
Very crystal clear explanation 👌 👏
In naive approach make csum = 0 for every iteration if i. and in inner for loop j must go from i to i+k-1.. also wsum = wsum -a[i-k]+a[i] i runs from i=k to i
The CLEANEST explanation of this concept ..... Best video for sliding windows till now
After the first loop there should be a -> msum=wsum;
Very awesome !!!!!! cleared all point to point.
at 8:50
the j loop should be looped from i to i+k
not i to k
finally I understand this concept some one as explained it so well I am subscribing
anirudh ravichander if he didnt choose music
Excellent!!
Thank you❤
KL RAHUL
You are the best teacher that I found to explain leetcode. There are so many videos but no one could explain it so nicely. Animation and Java code makes it so simple to understand. Thanks so much brother!
at 15:40
when you said remove 9
you actually removed 15
and when you said add 21
you actually added 26
the answer was right 48 but the process was wrong
Video is very informative and the way you explain is wonderful.
There is one mistake in program ,if max sum of array is in first window so in that case logic will failed because you couldn't compare first window sum with next window sum.
add below line at line no.-10
mSum=wSum;
What a lecture .... sir It's too nice. You cleared the concept very nicely.
At 19:41 shouldn't it be wsum=csum-a[i-k]+a[I];
I'm really wondering how such good content can be available for free.. Really appreciate your efforts and content❤❤.thank you team.
Good explanation and teaching style. Thank you so much.
never thought this would be that easier. Just wow explanation 🤩
a very clear explanation. thank you
Really sir , it was simply understanding approach to teach .
Thank you!!!!
amazing and right to the point explanation! Thanks!
Amazing sir for so long I was very confused about this topic but today I am very clear with this topic. Thank you sir thank you very much
You explained it so well. Simply Amazing !!!!Keep making such videos, it was truly helpful
Sir your explanations is the best🎉 you made the topic very easy
Thank you
for(int i = 0 ; i
Nice explanation !
This is a simple problem the main problem comes when interviewer ask that each sub array should contain distinct elements
wonderful explanation ,crystal clear Thanks a lot
Best explanation bout sliding windows ever. Thank you for the video!
This Video Was Amazing...Sir the concept of Sliding window was too good....
Do share the course in your college community. Let's help each student in understanding DSA.
really easy method thank you so much sir
Best detailed explanation of the algorithm. Great video. 👍 Keep it up.
thanks for clear concept
Your teaching skills is absolutely gorgeous sirrrrr
Explaination was very good. Literally loved the way you explained.
Thank you, this was amazing like your all videos.
You are the best sir. Thank you!
O(nk)
Thanks sir❤
Sir maxSum should be checked after first for loop also
for(int i=0;i
Your Explanation is top notch , with the visuals and effects ... thanks
Really amazed is this really for freee!!!
Please continue like this
Really well explained. Thankyou
beautiful elaboration of sliding window concept..
Extraordinary ❤
Good explanation with illustration
bohot shi sir me yesi video dhundh rha tha
Thanks really nice explanation and also visually explained. Any one can easily understand.
@TAP ACADEMY
I found the bug in the code
The optimized code(sliding window) fails if my input array is
10 10 10 5 2 3
and k=3;
expected output should be 30 (10+10+10)
but the output for above code will be coming out to be 25(10 + 10 +5)
this code wont work if the frst k length subarray have max sum so inorder to make it correct
after the frst loop you should update the msum i.e
msum=Math.max(wsum,msum);
try this test case
let me know if i m wrong!!
Explain was good but we have not initialize the currentsum , in explaination u r saying that we have to substract the starting of ith element but u r adding there .
thanks sir
24:59 you didn't add first window sum to mSum
8:48 is wrong with the 2nd loop of j, you have to update it to:
int sum=0;
int max =Integer.MIN_VALUE;
int j=0;
for(int i=0; i
Thanks bruh ❤
this code will give n^2 time complexity but in sliding window it should be 'n'
no it will give us O(n)
@@komalbharti7869
your explanation is such a nice one. I am one working profession and need to brush up all these algorithms for Interview Preparation. Please upload more examples of sliding window mechanism.
Nice explanation
Thank you! But your code ignores the first window max. So, mSum = wSum should be added before second loop.
What if for the first time wsum is greater than all subarrays?
It will skip the first hight sum of 0 to 2 array sizes
for (int i= k; n > i; i++){
wsum=wsum-a1[i-k]+a1[i];
msum=Math.max(msum,wsum);
}
Here we are picking up msum
after using msum=Math.max(msum,wsum)
so that the first 0 to 2 index sum will be skipped if it has greater sum of all subarrays
eg first wsum=52 instead of 42 ;
can you check it please
Great explanation👍
Sir plz continue the lectures !! They are very helpful to us
great video plz explain dynamic sliding window
i am not getting correct output for naive approach i am getting 113, i am not getting the what issue is. please let me know if anyone found in this code
package javaprog;
public class subarray_sum_nonNegative_integers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {2,9,31,-4,21,7};
int maxsum=Integer.MIN_VALUE;
int sum=0;
int k=3;
for(int i=0;i
what if i want to print the array which gave me that result???
You teaches really well, Epic
Great bro
sir are you a brother of KL RAHUL....
I understand this technique quite well now. No one can explain this technique in detail like you.
im impressed with your lecture keep continue it you will achieve what you except
8:44 after updating mSum inside i loop then at the next line change cSum again to 0 cSum=0;
Or else it adds up with the previous cSum