Two Pointer
Session Objective
• Understand what the Two Pointer
• Know when to apply it
• Be able to implement it in different scenarios
• Analyze its space complexity
• Practice
1. What is the Two Pointer?
• The two pointer technique uses two variables (pointers) to iterate
through data structures like arrays, vector and strings… in a
coordinated way to solve problems more efficiently.
2. Real-Life Analogy :
• Imagine a city street with mobile shops lined up from point A to point
B. You start from point A, your friend from point B — both searching
for the Apple store. No one knows exactly where it is, but each shop
you ask gives you a hint if it's ahead or behind. You keep moving
inward from both sides until you find it
Step Action
You start from point A, your friend starts from point B
1
1️⃣
(like two pointers).
You both check the current shop — it’s not Apple, but
2️⃣
each shopkeeper gives you a hint: "Go further inside."
Based on that, you move right and your friend moves
3️⃣
left.
You continue asking vendors as you get closer from
4️⃣
both sides.
You both meet at the Apple store — this is the
5️⃣ condition where both pointers converge and the target
is found.
May Student Question.
• What if they Cross each other???
If you and your friend cross each other, it simply means you’ve
checked every shop, but the Apple store doesn’t exist on the street
— maybe you were misinformed!
3. When Do We Use It?
• Sorted arrays
• Problems involving pairs, subarrays, or palindromes
• Efficient searching or partitioning
How the things work in coding.
• Given a sorted array of integers and a target sum (14), determine
whether there exists a pair of numbers in the array such that their
sum equals the target. Return true if the pair exist, false otherwise.
• 0 1 2 3 4 5
2 4 6 8 8 9
• I = 0, J = 5, Current Sum = 2 + 9 = 11 < 14, so move left pointer to right
side.
• 0 1 2 3 4 5
2 4 6 8 8 9
I = 1, J = 5, Current Sum = 13 < 14, move left pointer to right side.
2 4 6 8 8 9
I = 2, J = 5, Current Sum = 15 > 14 move right pointer to left side.
2 4 6 8 8 9
• I = 2, J = 4, Current Sum = 14 == 14(target) found return true.
Reversing an Array/Vector:
• 0 1 2 3 4
2 3 5 6 1
• I = 0, J = 4, swap(nums[0] , nums[4]) After Swapping left pointer move
right side and right pointer move left side
1 3 5 6 2
• I = 1, J = 3, swap(nums[1] , nums[3]) After Swapping left pointer move
right side and right pointer move left side
1 6 5 3 2
• I = 2, J = 2, Swap(nums[2], nums[3]) and left pointer move to right
side and left pointer move left side.
1 6 5 3 2
• Now they cross Each other so we say that whole array is traverse, just
stop the swapping operation.
May Student Question.
• What happens if the two pointers cross each other, and the operation
continue;
• Index out of bounds errors
• Incorrect logic or infinite loops
• Runtime Error if things are not handle properly
How Space Complexity is Optimized in Two
Pointer Technique
• Naive Approach (Need Extra Space) : Like I’ll take a example reversing an array: We are taking
new array of N size for the coping element from the end of the given array to new array from 0th
index to nth index
• Old array 3 5 1 6 2
• New array 2 6 1 5 3
May Student Question:
• Why It's Efficient:
• We reverse the array using only two variables — no need to copy or store
anything extra. That's what makes the space complexity O(1), or constant
• With two pointers, you're swapping in place — no backup needed. You're not
just saving time, you're saving memory too!
How Time Complexity is Optimized in Two
Pointer Approach
• Naïve Approach (O(N^2)) : Given a sorted array and a target sum,
return true if there are two numbers whose sum equals the target.
• 3 4 5 6 7 11
check = 5
• 3 4 5 6 7 11 check = 4
•
• 3 4 5 6 7 11 check = 3
3 4 5 6 7 11
• check = 2
• 3 4 5 6 7 11 check = 1
• From 0th index number of check is 5
• From 1th index number of check is 4
• From 2nd index number of check is 3
• From 3rd index number of check is 2
• From 4th index number of check is 1
• If count total number of check is = 5 + 4 + 3 + 2 + 1 = 15 which is total
number of pair of the array and tries every possible pair
• Although length of the array is 6 = N * (N - 1) / 2 = 6 * (6 – 1) / 2 = 15
• So time Complexity for the Naïve Approach is O (N^2) In worst case.
Now we will do coding
• Target Sum : Naïve Approach
• Time Complexity : O(N^2)
• Like you are tries all pair in the array
• And total number of pair is :
• N * (N – 1) / 2 ~~~ N ^ 2;
• Space Complexity : O(1), B’coz we are
Not using any extra space for that.
Now we will do coding
• Target Sum : Optimal Approach
• Time Complexity : O(N)
• Space Complexity : O(1), B’coz we are
Not using any extra space for that.
Now we will do coding
• Reversing An array : Naïve Approach
Time Complexity : O(N);
Space Complexity : O(N);
We are using extra space
to store the elements from
the end of the original array
into the beginning of a new array
Now we will do coding
• Reversing An Array : Optimal Approach
Time Complexity : O(N);
Space Complexity : O(1);
Now we will do coding
• Check Palindrome or not in a string.
• Definition of Palindrome: A string is a palindrome if it reads the same
forward and backward.
Examples: 12321, 12221, 12345
• Naïve Approach : (1). Create a reversed copy of the string. (2).
Compare the original string with the reversed one. (3) If both are
equal → It's a palindrome; otherwise → Not a palindrome.
• Time Complexity:
• O(n) where n is the length of the
string (due to reversing and comparison).
• Space Complexity:
• O(n) extra space for storing the reversed
string.
Now we will do coding
• Optimal Approach : Instead of creating a reversed string (which uses
extra space), we can use the two-pointer technique to compare
characters from the beginning and end moving toward the center.
• Logic:
• Use two pointers:
• left = 0
• right = n – 1
• Compare characters at left and right
• If they are not equal → Not a palindrome
• If equal → move left++ and right– until left >= right
• If all characters matched → It is a palindrome
• Time Complexity:
• O(n) — each character is checked only once
• Space Complexity:
• O(1) — no extra space used
Practice :
• 1. Revers String
• 2. Two Sum : Pair with given target
• 3. 3194
Thank You