*carried over the corrections from previous sliding window tutorial* 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; }
It is unfortunate that you have not been uploading from a few days. You have explained this concept really well. I dont remember studying this anywhere in any DSA course online or offline.
What a brautiful explanation sir, correct of pictures and respective code lines are used for the explanation which eases the process of understanding - which i haven't seen any other tutorials/ teachers on youtube doing! great!
The two sum is such a great problem! The solution of two pointers works just fine but it will fail in leetcode if submitted because it doesnt work for negative numbers!
Great job.. Just to be more accurate, at 17:22 -> second while time complexity must be n-k, not n. So, all whole algorithm will be work in exactly O(n).
Explanation in the video is great, I just want to point out one fact that for target = 6 the algorithm works fine. But if the target is 5 the algorithm returns the index [1,6] which can be incorrect if the question says to find minimum indices for the sum of two elements to get the target value. In that case, the indices should be [2,3]. Apart from that, the video was cool.
Your video gives me so much knowledge as well as beauty of programming. Keep it up, Sir! By the way, May you please upload 3sum and/or 4 sum problems as soon as possible?
Thanks a lot, brother for the lovely feedback. I hope, people will love and share my content so that your comment will be no more just a comment, will become reality. 🙂
Hey Every one, If you find this tutorial helpful, please do not forget to like , comment, share and It would be great if you can leave your feedback about the tutorial, as I have put a lot of hard work to make things easy for you. Thanks ..!! 🙏🙏
Sir actualy the two sum is failing at case of [3,2,4] bcoz when we move to first pointer as start=0 and end=length-1 so sum will be 7 then we will decrement end then it will point to two( index 1) then at last result will be [0,0] instead of [1,2] can you please rectify it !!!
Hi a simple doubt from this nood In the maximum sum problem what if maximum sum occurs at first window we are not assigning the first window's sum to the maximum sum variable..please reply its confusing me ...help me out Time is 16:40
hmmn in the example 2 provided at around 5:59. i dont understand why the output should be [3,4] not [2,3] since the element 3 is under index 2 and element 3 is also in index 3. am i missing something?
Here is the summary which may help. Let's learn together What is 2 pointers technique? - a technique for searching in loop using 2 indicators, especially for strings, arrays and linked lists - need to be used in sorted array / linked list Why should know it - help reduce time complexity types of two pointers: - opposite directional - equi directional
I have a question from you... In this question we have to find pair of number having sum = 6. Now the given array is a[1,2,3,3,4,5]....now I want 3 pairs as output (1,5),(2,4),(3,3)...now what should I do because in my output it is showing 1,5 only and comes out of the loop...?
Currently we don't have, but planning to collate my learning at one place in a sequence. So that people can more benefits. But will take some time to create such contents. Lets see.
We have multiple telegram channels and groups as well, you can post anything on channel it is used only for broadcasting but you can use group to discuss anything with your fellow coder. Join our telegram group as mentioned in video description to ask your query.
Hello sir, you said in equi-direction two-pointer problem, you mentioned, one pointer moves faster than the other, but the shown example, both pointers move at same speed. Why so?
Your sliding window solution is not working correctly when the first window is the biggest sum, because you're not evaluating the maxSum after the first while loop. Try changing the first element of the input array from 1 to 10 (which for k=4 would make windowSum=16) - the function will still return 13.
We have done the correction in our sliding window tutorial code and mentioned the same in a pinned comment of that video, please follow the pinned comment and let us know if still there is any doubt.
import java.util.Arrays; class Solution { public int[] twoSum(int[] nums, int target) { int start=0; int end=nums.length-1; int temp[]=new int[nums.length]; for(int i=0;i
Your algorithm will not work when the array has more than 2 numbers which is the same like 1,2,3,3,3,3,4,6 etc and we have return a list of possible solutions
improvise this one and it works- import java.util.Arrays; class Solution { public int[] twoSum(int[] nums, int target) { int start=0; int end=nums.length-1; int temp[]=new int[nums.length]; for(int i=0;i
yes but you can improvise this and use- import java.util.Arrays; class Solution { public int[] twoSum(int[] nums, int target) { int start=0; int end=nums.length-1; int temp[]=new int[nums.length]; for(int i=0;i
@@JitendraSingh-qd7jk this one is how you can use the same method for non sorted array. For eg if target is 6 and input array is {3,2,4} if you sort it and then return indices then it will give {0,2 } which is incorrect cause answer should be {1,2}.so this technique works.
@@ravibisht3300 You're just sorting using inbuilt method what's so new lol. The sorting takes a bit time complexity too. So this method can't work with O(1) in worst case.
nicely explained.. but i found one guy who has used exact same presentation in your video ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ymKrGndnTis.html see ..he's stealing your work
thanks for sharing.. just watch the video.. literally the presentation was exactly looks the same in his multiple videos. but glad to see that people are so much impressed with our presentation/style that they started copying that.