Overview
Two pointers do indices use karte hain, sorted arrays ke problems ek pass mein solve karte hain.
Analogy
Jaise detective book ke dono ends se ek saath padhna condition satisfy karne wali pair dhundne ke liye.
Step-by-step
- left ko start, right ko end par set karo
- Dono pointers milne tak loop karo
- Condition check karo aur answer update karo
- Condition ke hisaab se left badhao ya right ghatao
Visual
[1,2,3,4,6] target=9
left=0(1),right=4(6)→sum=7<9→left++
left=1(2),right=4(6)→sum=8<9→left++
left=2(3),right=4(6)→sum=9→found!
Common mistakes
- Unsorted array par use karna
- Pointers advance karne ki condition galat likhna
Practice questions
- Sorted array ke liye Two Sum implement karo
- Duplicate array mein 3Sum ke saare pairs nikalo