Sliding Window Technique | Google Coding Interview | Maximum Size SubArray Of Size K

Поділитися
Вставка
  • Опубліковано 21 січ 2025

КОМЕНТАРІ • 175

  • @JavaAidTutorials
    @JavaAidTutorials  5 років тому +17

    *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;
    }

    • @himanshujain7014
      @himanshujain7014 5 років тому +1

      Hello sir I am yr follower on Hacker rank ,I am also 6 star in Hacker rank but what to do next?

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +4

      choose another platform like codechef or codeforces and get the 6 star their also.

    • @arunkumarvenkatesan1470
      @arunkumarvenkatesan1470 5 років тому +1

      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

    • @himanshujain7014
      @himanshujain7014 5 років тому

      @@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?

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Did not get your query ? what idea you are talking about?

  • @robertsedgewick1266
    @robertsedgewick1266 4 роки тому +17

    Explaining tough problems with a dry explanation is easy and commonplace. Your work is brilliant because it makes tough problems seem easy to solve!

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +2

      Thanks a lot for this feedback.
      If you find this channel helpful , please do share among your friends.

  • @aeon7748
    @aeon7748 2 роки тому

    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.

  • @tentx652
    @tentx652 3 роки тому +1

    better understanding after watching the video...!! great going man

  • @vigneshhendrix4447
    @vigneshhendrix4447 4 роки тому +2

    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.

  • @pedestrian9287
    @pedestrian9287 5 років тому +5

    Excellent video and terrific explanation! Best video on this topic on UA-cam!

  • @aravinddesikamani
    @aravinddesikamani 5 років тому +12

    I am sure that your subscribers count will grow with exponential complexity.. Keep the great work up😇

  • @saravananj3898
    @saravananj3898 3 роки тому

    trust me this the best video u can find in youtube in this topic

  • @sureshgarine
    @sureshgarine 3 роки тому +1

    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,

  • @samrey8134
    @samrey8134 4 роки тому +1

    After 2 weeks at this, I family got it. You are the best. Thank you so much

  • @law5
    @law5 8 місяців тому

    Your explanation on this topic was very clear. Keep it up!

  • @guess_d2s344
    @guess_d2s344 2 роки тому +1

    Thank you for explaining this concept in really nice way.
    Awesome 👍👍

  • @venpro8705
    @venpro8705 4 роки тому +1

    Your explanation is best than any other youtubers
    Thanks for ur explanation sir.
    Keep going
    From chittoor AP

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +1

      So nice of you 😀
      If you find it useful, please do share with your friends as well. 🙏

  • @anushree3744
    @anushree3744 5 років тому +4

    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

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Thank you for providing such a nice feedback. Sure will start including the problems also.

  • @urunovtimes5816
    @urunovtimes5816 4 роки тому

    you are one of the my friend, from India. Oh my goodness. You are really clear explained in the wsa

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +1

      Thank you :)
      If you like our content please share with others too

  • @prasaad44
    @prasaad44 4 роки тому +1

    Superb bro..... pls continue more on this....

  • @elgatito00
    @elgatito00 3 роки тому +2

    Great explanation 🤝

  • @vaibhavikhachane6225
    @vaibhavikhachane6225 2 роки тому +4

    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;

  • @chrispure7461
    @chrispure7461 2 роки тому +1

    I love you man..great explanation.

  • @nandakumarr9288
    @nandakumarr9288 4 роки тому

    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
      @JavaAidTutorials  4 роки тому

      Thanks for the awesome 🔥 feedback 😊

    • @nandakumarr9288
      @nandakumarr9288 4 роки тому

      @@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

  • @ashutoshsingh5568
    @ashutoshsingh5568 3 роки тому

    Awesome explanation bro. Keep teaching DSA.
    God bless you.

  • @amitaher31
    @amitaher31 4 роки тому +1

    Very cool man, keep up the good work.. thanks for adding the correction.. I know many interviewers today look for these edge cases!

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Thanks for the feedback 😊,
      If you find the tutorial helpful, please do share with your friends also.

  • @Spider-Man_67
    @Spider-Man_67 2 роки тому

    Man...your explanations are epic

  • @subhamjain707
    @subhamjain707 5 років тому +1

    Great explanation..Just continue making such conceptual videos.Its going to help a lot.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Sure @Subham.
      If you find our channel helpful, please do share with your friends too.

  • @mongaslanguagetechnologies58
    @mongaslanguagetechnologies58 5 років тому +6

    Thanks. This is a very common interview question.

  • @pzkpfwvii7090
    @pzkpfwvii7090 4 роки тому +1

    Thank you for your videos. Respect man! You're Waffentrager E100 in algorithm world!

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Thanks 😊
      If you find this channel helpful, please support us by sharing it with your friends....🙏🙏

  • @kanchanjeswani2001
    @kanchanjeswani2001 4 роки тому +3

    Very nice video sir.Thank you so much..

  • @madhutalaari
    @madhutalaari 4 роки тому +1

    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!!

  • @vinothiniseenivasan1502
    @vinothiniseenivasan1502 2 роки тому +1

    thanks for crystal clear explanation.....

  • @arunkumarvenkatesan1470
    @arunkumarvenkatesan1470 5 років тому +2

    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

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Thanks for your feedback @arun.
      I am trying my best to teach you all.
      Stay tuned, for cool stuff.

  • @geetusharma3923
    @geetusharma3923 4 роки тому +1

    Very nice explanation . Thanks for valuable content ❤️👍 all the best

  • @vikasmastud7005
    @vikasmastud7005 5 років тому +1

    Very easy to understand and nice visual representation of sliding window techniq

  • @xotic37
    @xotic37 3 роки тому

    Great sir great great great I will Sher my friend your chanel link thank so mush sir really Good

  • @rajeshkumar-ty8nj
    @rajeshkumar-ty8nj 4 роки тому

    I have improved my coding skill through this channel.

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Glad to hear that.😊
      If you find our channel helpful, please support us by sharing it.

  • @saumyadeepdebnath6412
    @saumyadeepdebnath6412 3 роки тому

    Very nice explanation bhaiya... Thank you

  • @apnasid794
    @apnasid794 3 роки тому

    Good work sir keep it.👍

  • @TridibSamanta
    @TridibSamanta 5 років тому

    Awesome explanation ! Keep up the good work :)

  • @kesharwani.prashant
    @kesharwani.prashant 3 роки тому

    You are an amazing teacher!!!

  • @frozen_tortus
    @frozen_tortus 4 роки тому +1

    Very nicely explained. This looks like dynamic programming.

  • @benneal8224
    @benneal8224 4 роки тому

    Awesome explanation! Thanks for putting these up as they are very beneficial.

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Glad you like them!
      If you find it useful, please do share with other too.

  • @shivajunna
    @shivajunna 5 років тому +2

    Nice explanation !!

  • @akrammohammed1167
    @akrammohammed1167 4 роки тому +1

    Great explanation thanks for the wonderful video!

  • @pradeepjagtap7220
    @pradeepjagtap7220 5 років тому +2

    You are awesome! Thank you very much.

  • @zavier3436
    @zavier3436 3 роки тому +1

    Just awesome ❤️

  • @mahmoudabdelsattar8860
    @mahmoudabdelsattar8860 2 роки тому

    please more around these please cover all (BS, TEES, DP.....) please -> ur way is easy

  • @dipakvyawahare
    @dipakvyawahare 3 роки тому

    Best explanation!! thanks

  • @randomlyrandom7002
    @randomlyrandom7002 4 роки тому +1

    Great video sir!

  • @senthilkumaran5317
    @senthilkumaran5317 5 років тому

    Great explanation with visuals . Subscribed

  • @abhinavvgoyall
    @abhinavvgoyall 2 роки тому

    Best explanation!

  • @anshpandey14325
    @anshpandey14325 Рік тому +1

    GOOD EXPLANATION

  • @pritammishra9850
    @pritammishra9850 4 роки тому +1

    Amazing explanation

  • @Roy-zt7sl
    @Roy-zt7sl 4 роки тому

    Excellent presentation & explanation

  • @anupamachanamolu2678
    @anupamachanamolu2678 2 роки тому

    Great explanation.

  • @benmathew534
    @benmathew534 5 років тому +2

    You can also use a deque. just add to the end of the list and remove from the front

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      yes, using dequeue we can solve this problem if size is fixed but it will not work for dynamic size array.

  • @dheerajkushwaha1657
    @dheerajkushwaha1657 5 років тому +2

    Nice explanation

  • @venpro8705
    @venpro8705 4 роки тому +1

    Super bro👍

  • @rohancombay2689
    @rohancombay2689 Рік тому +1

    Damn Good man

  • @architpandita790
    @architpandita790 4 роки тому +1

    Nice Approach

  • @manojprabhu5641
    @manojprabhu5641 3 роки тому

    very usefull :)
    tq bro.

  • @rajgupta6738
    @rajgupta6738 2 роки тому +1

    you are amazing

  • @ishitaanand4283
    @ishitaanand4283 4 роки тому +1

    It really helped me a lot

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +1

      thank you. if you find it helpful, please do share with your friends as well.

    • @ishitaanand4283
      @ishitaanand4283 4 роки тому

      @@JavaAidTutorials sure Sir

  • @mayurravindra9433
    @mayurravindra9433 3 роки тому +1

    very helpful!

  • @musadhk
    @musadhk 3 роки тому +1

    nicely explained

  • @vanngaonguyen9348
    @vanngaonguyen9348 4 роки тому +1

    great explanation

  • @UECAshutoshKumar
    @UECAshutoshKumar Рік тому +1

    Thank you sir

  • @hellofriend3091
    @hellofriend3091 3 роки тому

    Thank you sir ❤️

  • @vinothkannans1910
    @vinothkannans1910 4 роки тому

    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.

  • @kinneravijay617
    @kinneravijay617 2 роки тому

    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

  • @prakashurkude4044
    @prakashurkude4044 2 роки тому +1

    it's easy to search with question number, so please add question number too in video title.

  • @shashwatshukla3289
    @shashwatshukla3289 3 роки тому

    Sir ,is prefix sum algorithm a good approach here?

  • @hemaladani4510
    @hemaladani4510 2 місяці тому

    Can someone please send link to this problem? Leetcode or hackerrank or something else?

  • @aishwaryaporwal2321
    @aishwaryaporwal2321 3 роки тому

    Plss sir also make a vedio on Dutch national flag algorithm technique

  • @Mike-mw1fu
    @Mike-mw1fu 5 років тому +1

    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

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      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.

  • @2cool238
    @2cool238 3 роки тому

    why does prefix sum not work for this problem

  • @RakeshKumar-en1uq
    @RakeshKumar-en1uq 2 роки тому

    Please try to put video based more DSA

  • @navinchainani4721
    @navinchainani4721 3 роки тому

    Sir instead of windowsum +=A[end]-A[end-k] we can write windowsum+=A[end]-k

  • @nileshmishra3192
    @nileshmishra3192 4 роки тому

    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 :)

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +1

      Hi @Nilesh, please have a look on pinnned comment to get the answer for your query.

  • @Abhijeetkumar-jr8fq
    @Abhijeetkumar-jr8fq 4 роки тому

    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

    • @Abhijeetkumar-jr8fq
      @Abhijeetkumar-jr8fq 4 роки тому

      between both loop put
      maxSum=windowSum

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      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.

  • @amarchoubey5615
    @amarchoubey5615 3 роки тому

    Super

  • @156sparsh7
    @156sparsh7 4 роки тому +1

    Thanks

  • @amitghosh2183
    @amitghosh2183 2 роки тому

    But array is not sorted. Then how can be answer 13 why not(9+7+3+2)=21.

  • @rishurajan8080
    @rishurajan8080 4 роки тому +2

    Hello sir,please may ypu provide source code in c++ as well

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +1

      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.

  • @sohampatra1207
    @sohampatra1207 4 роки тому

    sir can you make video on the largest rectangle&hackerrank solutions on stack game problem

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      I need some time to upload more content.
      Due to lockdown, i am stuck without my laptop 😞

  • @iezioaudi22
    @iezioaudi22 10 місяців тому

    Thanks!!!!!!

  • @DTWK
    @DTWK 3 роки тому +1

    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.

    • @Legenderboy123
      @Legenderboy123 3 місяці тому

      If the (array.length == k), then it will return 0 as maxSum.
      So, after first for loop, set
      maxSum = windowSum;

  • @jlecampana
    @jlecampana 5 років тому +1

    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

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      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."

  • @ccarnagee7867
    @ccarnagee7867 5 років тому

    please suggest some problems for practicing these algorithms...Codeforces.leetcode,hackerrank any platform..The more the better....

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      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

  • @start1learn-n171
    @start1learn-n171 7 місяців тому

    Tq

  • @dhaanumalavan1968
    @dhaanumalavan1968 2 роки тому

    👌👌

  • @pramitis2645
    @pramitis2645 4 роки тому

    May I connect you in faceboook Or anywhere else I need help regarding hackerrank questions

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      sure but don't expect from us to solve hackerrank problems for your assignment or coding interview.. 😀

  • @ztrixx3280
    @ztrixx3280 4 роки тому

    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..

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      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.

  • @akhilchowdary3161
    @akhilchowdary3161 5 років тому

    hey a small intimation that u are not comparing the first k values with maximum value

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      yes , we have missed it seems, just added as a part of correction in my pinned comment.
      Thanks for pointing it out..:)

  • @shivamshah431
    @shivamshah431 5 років тому +2

    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.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      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.

  • @swapnilbmkjs7610
    @swapnilbmkjs7610 2 роки тому +1

    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

  • @anamikaahmed4887
    @anamikaahmed4887 4 роки тому +2

    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.

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Thanks for your detailed feedback.
      we have already added these corrections long back in our pinned comment and git hub code😊.

  • @piotrekkrakowiak5060
    @piotrekkrakowiak5060 3 роки тому

    For God sake add subtitles or slow down when you speak

  • @pramitis2645
    @pramitis2645 4 роки тому

    Hello

  • @vamsibavanari7409
    @vamsibavanari7409 4 роки тому

    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

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      this problem has been already addressed and i have pinged my comment on top. please have a look.

  • @VerzapierZyGaming
    @VerzapierZyGaming 5 років тому

    this cannot be a coding interview problem for google, unless they ask you to do it in DP.. this is basically clickbait

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      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.
      ua-cam.com/video/XKu_SEDAykw/v-deo.html

  • @jay-rathod-01
    @jay-rathod-01 4 роки тому +1

    Don't click this😂