Two Pointer Algorithm | Two Sum Problem | Solve DS Problems in O(N) Time

  Рет қаралды 125,063

JAVAAID - Coding Interview Preparation

JAVAAID - Coding Interview Preparation

Күн бұрын

Пікірлер: 136
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
*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; }
@abcd-sf5ur
@abcd-sf5ur 4 жыл бұрын
please make video on recursion please
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
I have one playlist for recursion you can follow this- kzbin.info/aero/PLSIpQf0NbcCk4be21WNhPHrHMSxFndZtB
@valkon_
@valkon_ 3 жыл бұрын
Thanks for the video. You just need maxSum = windowSum; No reason to call Math.max again.
@patrickmayer9218
@patrickmayer9218 2 жыл бұрын
In short, two pointer algorithms are a nice way to reduce time complexity by only iterating throughout the container once. Thanks for the video!
@srafayal
@srafayal 2 жыл бұрын
Best ever explanation over this topic across youtube
@latasha2123
@latasha2123 2 жыл бұрын
This is the best eplanation I got for two pointer so far.
@nishilshah140692
@nishilshah140692 3 жыл бұрын
@17:05 windowSum += a[end++]; after this line we should add : maxSum = windowSum; In case if first window is the maximum.
@104_velladurai.j9
@104_velladurai.j9 7 ай бұрын
Yes I also noticed that
@danishuddin9752
@danishuddin9752 2 жыл бұрын
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!
@faizanusmani1039
@faizanusmani1039 3 жыл бұрын
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.
@eddiephiri1763
@eddiephiri1763 10 ай бұрын
Still relevant today! Clearly explained and easy to follow! Great work!
@satyakimandal6830
@satyakimandal6830 3 жыл бұрын
Well explaianed, helped a lot to understand the algorithm. Thank you so much for such tutorial.
@travel_ghost22
@travel_ghost22 3 жыл бұрын
You came to my life as a bhagwan bro...I was facing so much difficulty understanding this.Thanks very mcuh keep making such videos
@sunkari00
@sunkari00 5 жыл бұрын
Great Job Buddy... You made look the problem simple now and thx for explaining both brute force and optimal solution in a concise manner.
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thank you for you nice feedback. 🙂
@saswatapatra5919
@saswatapatra5919 2 жыл бұрын
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!
@hemaladani4510
@hemaladani4510 2 ай бұрын
Love your curated list of problems. Leetcode often mixes hard ones with easy ones.
@emotionization
@emotionization 3 жыл бұрын
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).
@BrandonHo
@BrandonHo 2 жыл бұрын
great video! regarding the leet code problems you list @4:00, Move Zeroes should be categorized under equi-directional problems
@Human_Evolution-
@Human_Evolution- 3 жыл бұрын
I like your stuff. Best visualizations for sliding windows I've ever seen.
@shobhitbajaj9667
@shobhitbajaj9667 5 жыл бұрын
Love your videos so much 😍👍👍👍👌 Can't express how these videos are helping me. Thanks so much
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thanks a lot for your such a nice feedback. If you find our channel helpful, please share with your friends also.
@abhishekhail6682
@abhishekhail6682 Жыл бұрын
Thankyou for crisp and Clear concept.
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
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 ..!! 🙏🙏
@notdumb3182
@notdumb3182 2 жыл бұрын
Great video mate. Keep this type of videos coming.
@robertsedgewick1266
@robertsedgewick1266 4 жыл бұрын
BEST explanation on youtube!
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Wow, thanks!
@ritomukherjee8389
@ritomukherjee8389 4 ай бұрын
In 2 aum I always used hashmaps till date but I can see with the TC = O(N) nut the SC = O(N) too cause of the extra space uaed for HashMap. I think will try to Master the 2 pointers and Sliding Window to get TC = O(N) but here SC = O(1) no extra space. Awesome thanks. Also gonna implement same in my office project too tomorrow, where I have to search between Ids of a different table saved as JSON obj( array json encoded ) in DB field to get rid of some normalization and make it faster
@shanmugamss6312
@shanmugamss6312 2 жыл бұрын
no words to express your content and knowledge sharing...keep going ...thank you so much
@AvinashKumar-sw2do
@AvinashKumar-sw2do 3 жыл бұрын
Excellent tutorial with very good diagrams for the Two Pointer Technique. Kudos to you !!!
@JavaAidTutorials
@JavaAidTutorials 3 жыл бұрын
Glad it was helpful!
@letzzvibe
@letzzvibe 2 жыл бұрын
Great explanations & graphics
@ayushbudhwani
@ayushbudhwani 4 жыл бұрын
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.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
A simple problem can turned out to be different one if we add any restriction , it will not remain same. 😀
@AdityaSingh-ql9ke
@AdityaSingh-ql9ke 3 жыл бұрын
yep,...this problem needed binary search , not 2 pointer
@prakashkaruppusamy3817
@prakashkaruppusamy3817 5 жыл бұрын
Hi Kanhaiya... your videos are useful for coding aspirants like us. Keep up the good work buddy 😊👏🏼
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thanks, Prakash for such nice feedback. Stay tuned for learning cool stuff.!!
@hamzakhan-ks4ry
@hamzakhan-ks4ry 5 ай бұрын
bro akheeeeeer..! fabulous.!
@sachinshukla1095
@sachinshukla1095 2 жыл бұрын
Awesome explanation bro please make more videos
@sawandeepgavel54
@sawandeepgavel54 4 жыл бұрын
Pretty clear explanation, great job Sir
@charantechgeek
@charantechgeek 2 жыл бұрын
great explaination sir. thanks.
@chandrashekhar9470
@chandrashekhar9470 5 жыл бұрын
Best Explanation Ever!!!
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thank you. 🙂
@thomaschamberlain3905
@thomaschamberlain3905 4 жыл бұрын
Very well explained. Thank you.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Glad you liked it
@0anant0
@0anant0 5 жыл бұрын
Nice explanation with diagrams!
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thank you. 🙂 If you find it useful, please do share with others.
@noorashrafabdelmawlashiha1206
@noorashrafabdelmawlashiha1206 4 жыл бұрын
Such a damn shame that you are not popular yet. We all should work on making you famous on KZbin . You deserve it
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
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. 🙂
@mohanbhati9595
@mohanbhati9595 4 жыл бұрын
@@JavaAidTutorials yes.
@jvilbre
@jvilbre Жыл бұрын
thank you for your video!!!
@avinashkumarsingh9915
@avinashkumarsingh9915 5 жыл бұрын
Wow, Awesome Explanation . Please cover more Array type problem.
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thank you very much. I am working on it. If you find our channel helpful, please support us by sharing our channel.
@JoselitoPerez-j7d
@JoselitoPerez-j7d Жыл бұрын
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?
@Mike-mw1fu
@Mike-mw1fu 5 жыл бұрын
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?
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thank you for your nice feedback.
@harmonicsaurav
@harmonicsaurav Жыл бұрын
Hi, great tutorial. Would the two pointer method for Two Sum Problem work for unsorted arrays?
@alexschlesinger6498
@alexschlesinger6498 4 жыл бұрын
Great tutorial, thank you!
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Glad it was helpful!
@geetusharma3923
@geetusharma3923 4 жыл бұрын
Nice video 👍🙂
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Thanks 😀
@charlesandrews8790
@charlesandrews8790 4 жыл бұрын
Great video! Thanks very much :)
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Glad you liked it!
@wise_wealth_builders
@wise_wealth_builders 4 жыл бұрын
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
@DrowsyRock
@DrowsyRock 4 жыл бұрын
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...?
@ashishburnwal1839
@ashishburnwal1839 4 жыл бұрын
@@DrowsyRock remove break statement inside if condition, I think it will work.
@DrowsyRock
@DrowsyRock 4 жыл бұрын
@@ashishburnwal1839 ok thank you
@bingo9875
@bingo9875 4 жыл бұрын
genius!!
@ArbindYadav-oc3zg
@ArbindYadav-oc3zg 5 жыл бұрын
Very good explanation. Hearty thankful for your beautiful contribution
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thanks a lot for such a wonderful comment.😊 If you find our channel helpful, please support us by sharing it..
@sasikumartangala5001
@sasikumartangala5001 4 жыл бұрын
Simply amazing
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Thanks a lot 😊
@seeboonsoo
@seeboonsoo 5 жыл бұрын
Awesome video!
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thanks 🙂
@vinothkannans1910
@vinothkannans1910 4 жыл бұрын
Great explanation 👍. Could you please make video for leetcode 1590.
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
thanks for the feedback, will try.
@sandeepkumawat4982
@sandeepkumawat4982 4 жыл бұрын
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
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
thanks for pointing this out, I have added a correction in my comment and pinned it on top. i hope it will help you.
@KnowUrislam-1
@KnowUrislam-1 2 жыл бұрын
sir is sliding window technique and equi pointer both same? because the algorithm u have shown is almost same for the both
@vamsinadh100
@vamsinadh100 4 жыл бұрын
I started following you
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Thank you. I hope you will not disappoint 🙂
@MSDhoni-yl7tf
@MSDhoni-yl7tf 5 жыл бұрын
Video was very helpful. Great work. Can you give some example of slow and fast pointers in same direction?
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
thanks for your feedback 🙂 Will upload some more tutorial on two pointer technique in same direction soon, stay tuned.!
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@welberserafim6806
@welberserafim6806 3 жыл бұрын
You're amazing
@JavaAidTutorials
@JavaAidTutorials 3 жыл бұрын
thank you :)
@Firstusee256
@Firstusee256 5 жыл бұрын
Great work
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thank you for appreciating my work.
@shubhamk840
@shubhamk840 4 жыл бұрын
THANK YOU SO MUCH
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Welcome 😊
@vamsikrishnasai1682
@vamsikrishnasai1682 3 жыл бұрын
Great explanation but when we give input 0 1 2 16 2 2 3 and target 18 then we will get wrong result using two pointer recnique
@prithvini04
@prithvini04 4 жыл бұрын
So, if the array is sorted we can use two pointer technique else we should use sliding window technique for O(n) time complexity,right ?
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
It depends on the problem, but two pointer can be used when array is ordered
@shenth27
@shenth27 4 жыл бұрын
I think sliding window technique is a special case which allows you to find contiguous subarray satisfying a condition.
@tharunb754
@tharunb754 4 жыл бұрын
sir, what methodology is the best if the array is not in sorted order and if there is restriction of sorting ????
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
two pointer technique will be useful , when array is ordered some or the other way else it will not help you much.
@albertpraveenr1132
@albertpraveenr1132 Жыл бұрын
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 !!!
@vYadav16380
@vYadav16380 Жыл бұрын
thnks buddy
@memesmacha61
@memesmacha61 4 жыл бұрын
Hey bro u have any course on udemy or any other platforms ..I want more stuff about ds
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
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.
@nagendrapp2213
@nagendrapp2213 4 жыл бұрын
@@JavaAidTutorials start as soon as possible dsa +cp related from scratch : )
@melchizedekodonkor
@melchizedekodonkor 5 жыл бұрын
@JAVAAID can you enable us to ask questions on the telegram page?
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
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.
@dagabangel
@dagabangel 5 жыл бұрын
5 star rated
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
Thank you..😊
@shivangsaini3940
@shivangsaini3940 2 жыл бұрын
Great
@pravinchukkala545
@pravinchukkala545 2 жыл бұрын
Hi, Javaaid, Two pointers algorithm goes wrong. if we pass input like these [3,2,4] and target - 6
@ravibisht3300
@ravibisht3300 2 жыл бұрын
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
@logto2209
@logto2209 2 жыл бұрын
test case is not sorted. above code only runs in sorted array
@mohilkhare1708
@mohilkhare1708 4 жыл бұрын
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?
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
It means one pointer will always ahead of another pointer. But later may shift one by one.
@rishabhsaini3357
@rishabhsaini3357 2 жыл бұрын
thankyou bhai
@skvello
@skvello 5 жыл бұрын
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.
@JavaAidTutorials
@JavaAidTutorials 5 жыл бұрын
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.
@Akashgupta-id3kw
@Akashgupta-id3kw 2 жыл бұрын
class Solution { public int[] twoSum(int[] nums, int target) { int[] RArray = new int[2]; int start=0,end=nums.length-1; while(start < end) { int sum=nums[start]+nums[end]; if(sum == target) { RArray[0]=start; RArray[1]=end; break; } else if(sum > target) { end--; } else { start++; } } return RArray; } } THIS IS TWO POINTERS OPPOSITE DIRECTIONAL APPOROACH BUT IT'S SHOWING ERROR WHEN EXECUTING?? WHY
@harshavardhanreddy6236
@harshavardhanreddy6236 8 ай бұрын
yeah ! ..... for me also time limit is exceeding showing...
@rishiraj2548
@rishiraj2548 2 жыл бұрын
👍
@ejbjms
@ejbjms 2 жыл бұрын
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
@yourGuy675
@yourGuy675 3 жыл бұрын
sir but two sum problem in leetcode is unsorted
@charlesbaiden3236
@charlesbaiden3236 4 жыл бұрын
I didn't quite get why you returned plus 1. Can someone explain? Thanks
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
Can you please mention the time as well? where you have doubt.
@charlesbaiden3236
@charlesbaiden3236 4 жыл бұрын
JAVAAID - Coding Interview Preparation I understand, index is one based 10:49
@Beacher108
@Beacher108 3 жыл бұрын
Shouldn't index start from 0?!
@mB-cv3fj
@mB-cv3fj 3 ай бұрын
Start = -1 right??
@Rahul-oy4bp
@Rahul-oy4bp 2 жыл бұрын
This does not work for array that is not sorted.
@ravibisht3300
@ravibisht3300 2 жыл бұрын
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
@appikeeru5785
@appikeeru5785 6 ай бұрын
I didn't understand subarray
@JitendraSingh-qd7jk
@JitendraSingh-qd7jk 2 жыл бұрын
This technique works only in sorted array. :||
@ravibisht3300
@ravibisht3300 2 жыл бұрын
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
@JitendraSingh-qd7jk 2 жыл бұрын
@@ravibisht3300 I know how to sort an array.
@ravibisht3300
@ravibisht3300 2 жыл бұрын
@@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.
@JitendraSingh-qd7jk
@JitendraSingh-qd7jk 2 жыл бұрын
@@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.
@sachiiinnnn9734
@sachiiinnnn9734 4 жыл бұрын
nicely explained.. but i found one guy who has used exact same presentation in your video kzbin.info/www/bejne/r56uo3qkmdOHn9U see ..he's stealing your work
@JavaAidTutorials
@JavaAidTutorials 4 жыл бұрын
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.
@DevulapallySiddartha
@DevulapallySiddartha 4 ай бұрын
import java.util.*; class HelloWorld { public static void main(String[] args) { int[] arr = {1,9,-1,-2,7,3,-1,2}; // Acsending Order int k = 4; int temp = 0; int l = 0; int ans = 0; int startIndex = 0; for(int i=0;ians){ ans = temp; startIndex = l; } } } int[] result = new int[k]; for(int i=0;i
Sliding Window Technique | Google Coding Interview | Maximum Size SubArray Of Size K
16:06
JAVAAID - Coding Interview Preparation
Рет қаралды 67 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Two Pointer Approach - CP TacTics | Two Sum Problem | The Code Mate
15:45
How to Use the Two Pointer Technique
10:56
Team AlgoDaily
Рет қаралды 121 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 817 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 570 М.
Sliding Window Technique - Algorithmic Mental Models
36:45
Ryan Schachte
Рет қаралды 375 М.
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 249 М.